首页 > 解决方案 > R Studio 无法正确读取 txt 文件中的汉字

问题描述

当我尝试使用 读取 txt 文件时read.table(),在 Rstudio 中查看数据集时遇到了问题。原始txt.file由三列数据组成,包括ID、内容(粤语)和时间,格式如下:

100008251304976 你又知喎 2019-10-04 16:52:15

100027970365477 甘你买多几包花生,小心热气 2019-10-04 16:23:43

我编写了将其读入 Rstudio 的代码

x = read.table('comment.txt', encoding = 'utf-8', quote = "",fill = T,sep = '\t')

但结果是混乱的数据。

ç”∼ä½ è²·å¤šå¹¾åŒ…èŠ±ç”Ÿï¼Œå°å¿ƒç†±æ°£ 2019å¹´10æ

然后我检查了我的envlocale如下

sessionInfo()
#R version 3.6.1 (2019-07-05)
#Platform: x86_64-w64-mingw32/x64 (64-bit)
#Running under: Windows 10 x64 (build 18362)

#Matrix products: default

#locale:
#[1] LC_COLLATE=English_Hong Kong SAR.1252  LC_CTYPE=English_Hong Kong SAR.1252   
#[3] LC_MONETARY=English_Hong Kong SAR.1252 LC_NUMERIC=C                          
#[5] LC_TIME=English_Hong Kong SAR.1252    

#attached base packages:
#[1] stats     graphics  grDevices utils     datasets  methods   base     

#loaded via a namespace (and not attached):
#[1] compiler_3.6.1   rsconnect_0.8.16 tools_3.6.1      tinytex_0.16     xfun_0.10       
#[6] packrat_0.5.0  

Sys.getlocale()
# "LC_COLLATE=English_Hong Kong SAR.1252;LC_CTYPE=English_Hong Kong SAR.1252;LC_MONETARY=English_Hong Kong SAR.1252;LC_NUMERIC=C;LC_TIME=English_Hong Kong SAR.1252"

Sys.getenv("LANG")
# "C.UTF-8"

任何想法为什么我无法正确加载 txt 文件?顺便说一句,我可以print在 Rstudio 中输入或繁体中文。

print("試試")
# [1] "試試"

标签: character-encodingrstudio

解决方案


输入文件(在我的本地语言环境中添加了一行):

100008251304976 Třiatřicet žlutých šišinek  2019-10-04 16:52:15
100008251304976 你又知喎    2019-10-04 16:52:15
100027970365477 甘你買多幾包花生,小心熱氣   2019-10-04 16:23:43

R 代码片段(转换数据帧的各个行x可以循环完成,我知道......):

sessionInfo()

library(stringi)
library(magrittr)

x <- read.table('d:\\bat\\R\\comment.txt', encoding = 'UTF-8', quote = "\"", fill = TRUE, sep = '\t')

print(x)

x['V2'][1,] %>% 
  stri_replace_all_regex("<U\\+([[:alnum:]]+)>", "\\\\u$1") %>% 
  stri_unescape_unicode() %>% 
  stri_enc_toutf8()
x['V2'][2,] %>% 
  stri_replace_all_regex("<U\\+([[:alnum:]]+)>", "\\\\u$1") %>% 
  stri_unescape_unicode() %>% 
  stri_enc_toutf8()
x['V2'][3,] %>% 
  stri_replace_all_regex("<U\\+([[:alnum:]]+)>", "\\\\u$1") %>% 
  stri_unescape_unicode() %>% 
  stri_enc_toutf8()

结果(将代码片段粘贴到打开的 Rstudio 控制台):

> sessionInfo()
R version 3.4.1 (2017-06-30)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 18363)

Matrix products: default

locale:
[1] LC_COLLATE=Czech_Czechia.1250  LC_CTYPE=Czech_Czechia.1250    LC_MONETARY=Czech_Czechia.1250
[4] LC_NUMERIC=C                   LC_TIME=Czech_Czechia.1250    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] magrittr_1.5  stringi_1.1.5

loaded via a namespace (and not attached):
[1] compiler_3.4.1 tools_3.4.1   
> library(stringi)
> library(magrittr)
> 
> x <- read.table('d:\\bat\\R\\comment.txt', encoding = 'UTF-8', quote = "\"", fill = TRUE, sep = '\t')
> 
> print(x)
            V1                                                                                                V2
1 1.000083e+14                                                                        Třiatřicet žlutých šišinek
2 1.000083e+14                                                                  <U+4F60><U+53C8><U+77E5><U+558E>
3 1.000280e+14 <U+7518><U+4F60><U+8CB7><U+591A><U+5E7E><U+5305><U+82B1><U+751F>,<U+5C0F><U+5FC3><U+71B1><U+6C23>
                   V3
1 2019-10-04 16:52:15
2 2019-10-04 16:52:15
3 2019-10-04 16:23:43
> 
> x['V2'][1,] %>% 
+   stri_replace_all_regex("<U\\+([[:alnum:]]+)>", "\\\\u$1") %>% 
+   stri_unescape_unicode() %>% 
+   stri_enc_toutf8()
[1] "Třiatřicet žlutých šišinek"
> x['V2'][2,] %>% 
+   stri_replace_all_regex("<U\\+([[:alnum:]]+)>", "\\\\u$1") %>% 
+   stri_unescape_unicode() %>% 
+   stri_enc_toutf8()
[1] "你又知喎"
> x['V2'][3,] %>% 
+   stri_replace_all_regex("<U\\+([[:alnum:]]+)>", "\\\\u$1") %>% 
+   stri_unescape_unicode() %>% 
+   stri_enc_toutf8()
[1] "甘你買多幾包花生,小心熱氣"
>

使用接受的答案将 utf8 代码点字符串转换为 utf8


推荐阅读