gpt4 book ai didi

r - R 版本 4.2.0 中的 read.csv 弹出 "invalid multibyte string 8"错误

转载 作者:行者123 更新时间:2023-12-03 08:06:33 31 4
gpt4 key购买 nike

我安装了全新的 R 版本 4.2.0 并尝试运行用版本 4.1.x 编写的代码。

当使用 read.csv 读取数据时,弹出了这个新错误:

Error in make.names(col.names, unique = TRUE) :invalid multibyte string 8

我认为这与新的 native UTF-8 支持有关?

我在支持英语的 Windows 11 下运行 R,我不知道 csv 文件中有任何特殊字符,但我也不能完全排除它,因为它是一个相当大的文件。

如何切换回运行时没有任何错误的旧编码?

最佳答案

default R 对于 < 4.2 版本的行为是:

If you don't set a default encoding, files will be opened using UTF-8(on Mac desktop, Linux desktop, and server) or the system's defaultencoding (on Windows).

此行为有 changed在 R 4.2 中:

R 4.2 for Windows will support UTF-8 as native encoding

要查找 Windows 10 上的默认编码,请运行以下 Powershell 命令:

[System.Text.Encoding]::Default

我的 Windows 10 计算机上的输出是:

IsSingleByte      : True
BodyName : iso-8859-1
EncodingName : Western European (Windows)
HeaderName : Windows-1252
WebName : Windows-1252
WindowsCodePage : 1252
IsBrowserDisplay : True
IsBrowserSave : True
IsMailNewsDisplay : True
IsMailNewsSave : True
EncoderFallback : System.Text.InternalEncoderBestFitFallback
DecoderFallback : System.Text.InternalDecoderBestFitFallback
IsReadOnly : True
CodePage : 1252

这可以传递给 read.csv 作为要使用的编码:

read.csv(path_to_file, encoding = "windows-1252")

如果您不确定如何将 Powershell 的输出转换为相关字符串,您可以使用 stringi 包搜索所有编码的列表:

# Replace "1252" with the relevant output from the Powershell command
cat(grep("1252", stringi::stri_enc_list(simplify = FALSE), value = TRUE, ignore.case = TRUE))

您可以从输出中的任何选项中进行选择:

# c("ibm-1252", "ibm-1252_P100-2000", "windows-1252") c("cp1252", "ibm-5348", "ibm-5348_P100-1997", "windows-1252")

关于r - R 版本 4.2.0 中的 read.csv 弹出 "invalid multibyte string 8"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72168709/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com