(R) 使用中文字符/ UTF-8和窗口10保存数据(矢量或数据帧)



我正在尝试保存从包含一些中文字符的网站下载的一些数据。我尝试了很多事情都没有成功。R studio默认文本编码设置为UTF-8,Windows 10区域也设置为Beta,使用Unicode UTF-8进行全球语言支持。 以下是重现该问题的代码:


##package used
library(jiebaR) ##here for file_coding
library(htm2txt) ## to get the text
library(httr) ## just in case
library(readtext)
##get original text with chinese character
mytxtC <- gettxt("https://archive.li/wip/kRknx")
##print to check that chinese characters appear
mytxtC
##try to save in UTF-8
write.csv(mytxtC, "csv_mytxtC.csv", row.names = FALSE, fileEncoding = "UTF-8")
##check if it is readable
read.csv("csv_mytxtC.csv", encoding = "UTF-8")
##doesn't work, check file encoding
file_coding("csv_mytxtC.csv")
## answer: "windows-1252"
##try with txt
write(mytxtC, "txt_mytxtC.txt")
toto <- readtext("txt_mytxtC.txt")
toto[1,2]
##still not, try file_coding
file_coding("txt_mytxtC.txt")
## "windows-1252" ```
For information
``` Sys.getlocale()
[1] "LC_COLLATE=French_Switzerland.1252;LC_CTYPE=French_Switzerland.1252;LC_MONETARY=French_Switzerland.1252;LC_NUMERIC=C;LC_TIME=French_Switzerland.1252" ```

我更改了设置本地,它似乎正在工作。 我只是在代码开头添加了这一行:Sys.setlocale("LC_CTYPE","chinese")

只需要记住最终将其改回来。尽管如此,我还是觉得很奇怪,这条线使得使用 UTF-8 进行保存成为可能,而以前是不可能的......

这在Windows上对我有用:

下载文件 :

download.file("https://archive.li/wip/kRknx", destfile="external_file", method="libcurl")

输入文本 :

my_text <- readLines("external_file")  # readLines(url) works as well

检查 UTF8 :

> sum(validUTF8(my_text)) == length(my_text)
[1] TRUE

您还可以检查文件:

> validUTF8("external_file")
[1] TRUE

这是我在Windows上注意到的唯一区别:

user@somewhere:~/Downloads$ file external_file 
external_file: HTML document, UTF-8 Unicode text, with very long lines, with CRLF line terminators

user@somewhere:~/Downloads$ file external_file 
external_file: HTML document, UTF-8 Unicode text, with very long lines

最新更新