Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
573 views
in Technique[技术] by (71.8m points)

rstudio - R Studio can not read chinese character in txt file properly

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

100027970365477 甘你買多幾包花生,小心熱氣 2019-10-04 16:23:43

I wrote the code to read it into Rstudio

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

but the result is messey data.

?”??? è2·?¤??1???…è?±?”?????°?????±?°£ 2019?1′10?

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"

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] "試試"
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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

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 = '')

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()

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 = '')
> 
> print(x)
            V1                                                                                                V2
1 1.000083e+14                                                                        T?iat?icet ?lutych ?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 ?lutych ?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] "甘你買多幾包花生,小心熱氣"
>

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...