部分中文字無法正確輸出到文件

608 views
Skip to first unread message

cheukkin ng

unread,
Mar 14, 2018, 4:51:40 AM3/14/18
to R軟體使用者論壇


如圖,部分中文字如"綫"輸出成txt file或CSV後, 會變成"<U+7DAB>"的unicode編碼

請問有方法解決嗎,謝謝!

WEPA ^_^

unread,
Mar 14, 2018, 11:26:54 AM3/14/18
to R軟體使用者論壇
Hi Cheukkin,

範例中 a1為正常顯示, a2 為模擬有亂碼結果, a3 使用locale, a4 使用 iconv.



# locale problem
# 2018.3.14
Sys.getlocale()
rm(list=ls())
a1 <- "線"
a1
write.csv(a1, file = "MyData1.csv")

Sys.setlocale("LC_ALL", "C")
Sys.getlocale()
a2 <- "線"
a2
write.csv(a2, file = "MyData2.csv")

# method 1 修改 locale
Sys.setlocale(category = "LC_ALL", "Chinese (Traditional)_Taiwan.950")
Sys.getlocale()
a3 <- "線"
a3
write.csv(a3, file = "MyData3.csv")

# method 2 使用 iconv
Sys.setlocale("LC_ALL", "C")
Sys.getlocale()
a4 <- "線"
a4
a4 <- iconv(a4, "UTF-8", "BIG-5")
a4
write.csv(a4, file = "MyData4.csv")
# end

cheukkin ng於 2018年3月14日星期三 UTC+8下午4時51分40秒寫道:

cheukkin ng

unread,
Mar 14, 2018, 10:48:12 PM3/14/18
to R軟體使用者論壇
Hi Wepa,
謝謝你的幫忙,但是我method 1 及method 2都不成功..

rm(list=ls())

Sys.getlocale()

Sys.setlocale(category="LC_ALL","Chinese (Traditional)_Hong Kong S.A.R..950")

a1 <- "綫"

a1

write.csv(a1, file = "MyData1.csv")


a2 <- "綫"

a2

a2 <- iconv(a2,"UTF-8","BIG-5")

a2

write.csv(a2, file = "MyData2.csv")


a3 <- "線"

a3

write.csv(a3, file = "MyData3.csv")

cheukkin ng

unread,
Mar 14, 2018, 11:04:44 PM3/14/18
to R軟體使用者論壇
我也把系統語言改成跟你一樣的,結果也是一樣


WEPA ^_^

unread,
Mar 19, 2018, 11:42:05 AM3/19/18
to R軟體使用者論壇
Hi Cheukkin,

您的作業系統是是Windows 7-32位元? 語系是繁體中文版? 另 sessionInfo() 結果為何? 安裝最新版R / RStudio 會有改善?

cheukkin ng於 2018年3月15日星期四 UTC+8上午11時04分44秒寫道:
我也把系統語言改成跟你一樣的,結果也是一樣


cheukkin ng

unread,
Mar 20, 2018, 5:49:07 AM3/20/18
to R軟體使用者論壇
window7 64bit 繁體中文版
剛出的3.4.4版本還沒嘗試

> sessionInfo()
R version 3.4.3 (2017-11-30)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1

Matrix products: default

locale:
[1] LC_COLLATE=Chinese (Traditional)_Hong Kong S.A.R..950  LC_CTYPE=Chinese (Traditional)_Hong Kong S.A.R..950   
[3] LC_MONETARY=Chinese (Traditional)_Hong Kong S.A.R..950 LC_NUMERIC=C                                          
[5] LC_TIME=Chinese (Traditional)_Hong Kong S.A.R..950    

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

other attached packages:
[1] tmcn_0.2-12

loaded via a namespace (and not attached):
[1] compiler_3.4.3 tools_3.4.3    yaml_2.1.14   

cheukkin ng

unread,
Mar 22, 2018, 2:25:31 AM3/22/18
to R軟體使用者論壇
好像是R與window之間的bug

txt <- "在綫"
writeLines(txt, "test.txt", useBytes=T)

輸出txt file時加上useBytes=T就可以正常顯示,但沒法根治問題
像輸出去SQL database或其他格式仍然沒法正常顯示
Reply all
Reply to author
Forward
0 new messages