我如何在 fwrite() 中指定编码以导出 csv 文件 R?
How can i specify encode in fwrite() for export csv file R?
由于 fwrite()
无法应用编码参数 ,我如何才能以与 fwrite()
一样快的速度导出特定编码的 csv 文件? (fwrite()
是目前我确认中最快的函数)
fwrite(DT,"DT.csv",encoding = "UTF-8")
Error in fwrite(DT, "DT.csv", encoding = "UTF-8") :
unused argument (encoding = "UTF-8")
你应该 post 一个可重现的例子,但我想你可以通过确保 DT
中的数据是 R 中的 UTF-8 格式,然后设置每列的编码来做到这一点至 "unknown"。然后,当您将数据写出时,R 将假定数据以本机编码进行编码。
例如,
DF <- data.frame(text = "á", stringsAsFactors = FALSE)
DF$text <- enc2utf8(DF$text) # Only necessary if Encoding(DF$text) isn't "UTF-8"
Encoding(DF$text) <- "unknown"
data.table::fwrite(DF, "DF.csv", bom = TRUE)
如果 DF
的列是因数,您需要先将它们转换为字符向量。
在撰写本文时,fwrite 不支持强制编码。我使用了一种解决方法,但它比我想要的要迟钝一点。例如:
readr::write_excel_csv(DT[,0],"DT.csv")
data.table::fwrite(DT,file = "DT.csv",append = T)
第一行将仅将数据 table 的 headers 保存到 CSV,默认为 UTF-8,字节顺序标记需要让 Excel 知道文件编码为 UTF-8。 fwrite 语句然后使用附加选项向原始 CSV 添加额外的行。这保留了 write_excel_csv 的编码,同时最大化写入速度。
如果你在 R 中工作,
试试这个作为工作方法:
# You have DT
# DT is a data.table / data.frame
# DT$text contains any text data not encoded with 'utf-8'
library(data.table)
DT$text <– enc2utf8(DT$text) # it forces underlying data to be encoded with 'utf-8'
fwrite(DT, "DT.csv", bom = T) # Then save the file using ' bom = TRUE '
希望对您有所帮助。
我知道有些人已经回答了,但我想使用 user2554330 的回答贡献一个更全面的解决方案。
# Encode data in UTF-8
for (col in colnames(DT)) {
names(DT) <- enc2utf8(names(DT)) # Column names need to be encoded too
DT[[col]] <- as.character(DT[[col]]) # Allows for enc2utf8() and Encoding()
DT[[col]] <- enc2utf8(DT[[col]]) # same as users' answer
Encoding(DT[[col]]) <- "unknown"
}
fwrite(DT, "DT.csv", bom = T)
# When re-importing your data be sure to use encoding = "UTF-8"
DT2 <- fread("DT.csv", encoding = "UTF-8")
# DT2 should be identical to the original DT
这应该适用于 data.table
上任何位置的任何和所有 UTF-8 字符
由于 fwrite()
无法应用编码参数 ,我如何才能以与 fwrite()
一样快的速度导出特定编码的 csv 文件? (fwrite()
是目前我确认中最快的函数)
fwrite(DT,"DT.csv",encoding = "UTF-8")
Error in fwrite(DT, "DT.csv", encoding = "UTF-8") :
unused argument (encoding = "UTF-8")
你应该 post 一个可重现的例子,但我想你可以通过确保 DT
中的数据是 R 中的 UTF-8 格式,然后设置每列的编码来做到这一点至 "unknown"。然后,当您将数据写出时,R 将假定数据以本机编码进行编码。
例如,
DF <- data.frame(text = "á", stringsAsFactors = FALSE)
DF$text <- enc2utf8(DF$text) # Only necessary if Encoding(DF$text) isn't "UTF-8"
Encoding(DF$text) <- "unknown"
data.table::fwrite(DF, "DF.csv", bom = TRUE)
如果 DF
的列是因数,您需要先将它们转换为字符向量。
在撰写本文时,fwrite 不支持强制编码。我使用了一种解决方法,但它比我想要的要迟钝一点。例如:
readr::write_excel_csv(DT[,0],"DT.csv")
data.table::fwrite(DT,file = "DT.csv",append = T)
第一行将仅将数据 table 的 headers 保存到 CSV,默认为 UTF-8,字节顺序标记需要让 Excel 知道文件编码为 UTF-8。 fwrite 语句然后使用附加选项向原始 CSV 添加额外的行。这保留了 write_excel_csv 的编码,同时最大化写入速度。
如果你在 R 中工作,
试试这个作为工作方法:
# You have DT
# DT is a data.table / data.frame
# DT$text contains any text data not encoded with 'utf-8'
library(data.table)
DT$text <– enc2utf8(DT$text) # it forces underlying data to be encoded with 'utf-8'
fwrite(DT, "DT.csv", bom = T) # Then save the file using ' bom = TRUE '
希望对您有所帮助。
我知道有些人已经回答了,但我想使用 user2554330 的回答贡献一个更全面的解决方案。
# Encode data in UTF-8
for (col in colnames(DT)) {
names(DT) <- enc2utf8(names(DT)) # Column names need to be encoded too
DT[[col]] <- as.character(DT[[col]]) # Allows for enc2utf8() and Encoding()
DT[[col]] <- enc2utf8(DT[[col]]) # same as users' answer
Encoding(DT[[col]]) <- "unknown"
}
fwrite(DT, "DT.csv", bom = T)
# When re-importing your data be sure to use encoding = "UTF-8"
DT2 <- fread("DT.csv", encoding = "UTF-8")
# DT2 should be identical to the original DT
这应该适用于 data.table
上任何位置的任何和所有 UTF-8 字符