R Studio无法正确读取txt文件中的中文字符 [英] R Studio can not read chinese character in txt file properly

查看:219
本文介绍了R Studio无法正确读取txt文件中的中文字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

While i was trying to read a txt file with read.table(), I met problems viewing the dataset in Rstudio. The original txt.file consists of three columns data including ID, Content(Cantonese) and Time, like the following format:


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

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

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

I wrote the code to read it into Rstudio

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

但结果是梅西数据。


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

ç"˜ä½ 買多幾包花ç"Ÿï¼Œå°å¿ƒç†±æ°£ 2019å¹´10æ

然后我检查了我的 env locale 如下

Then i checked my env and locale as follows

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文件的想法正确吗?顺便说一句,我能够在Rstudio中输入或打印 print 繁体中文。

Any ideas why I can not load txt file properly? By the way, i am able to tpye or print traditional Chinese in the Rstudio.

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


推荐答案

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

Input file (added a line in my native locale):

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 数据帧的各个行可以循环完成,我知道...):

R code snippet (converting individual rows of the x data frame could be done in a loop, I know…):

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控制台中):

Result (paste the code snippet to an open Rstudio console):

> 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 的可接受答案。

Used the accepted answer to convert utf8 code point strings like to utf8.

这篇关于R Studio无法正确读取txt文件中的中文字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆