在rApache中运行R时,语言环境是从Apache Web服务器继承的,因此Sys.getlocale()始终等于"C".我想使用我的Web应用程序UTF8,所以我使用:
Sys.setlocale("LC_ALL", 'en_US.UTF-8')
Run Code Online (Sandbox Code Playgroud)
但是,这不适用于没有此区域设置的计算机:
1: Setting LC_CTYPE failed, using "C"
2: Setting LC_COLLATE failed, using "C"
3: Setting LC_TIME failed, using "C"
4: Setting LC_MESSAGES failed, using "C"
5: Setting LC_MONETARY failed, using “C”
Run Code Online (Sandbox Code Playgroud)
有没有办法Sys.setlocale将语言环境设置为系统默认值UTF-8?即在Windows或德语Linux上也可以使用的东西?
我的系统:win7 + R-3.0.2.
> Sys.getlocale()
[1] "LC_COLLATE=Chinese (Simplified)_People's Republic of China.936;LC_CTYPE=Chinese
(Simplified)_People's Republic of China.936;LC_MONETARY=Chinese (Simplified)_People's
republic of China.936;LC_NUMERIC=C;LC_TIME=Chinese (Simplified)_People's Republic of China.936"
Run Code Online (Sandbox Code Playgroud)
在microsoft记事本中保存了两个具有相同内容的文件:一个保存为ansi格式,另一个保存为utf8格式.数据是M370马来西亚航空公司的死亡名称.或者您可以通过这种方式创建文件.
1)将数据复制到microsoft记事本中.
????,??,????
HuangTianhui,?,1948/05/28
???,?,1952/03/27
???,?,1994/12/09
Run Code Online (Sandbox Code Playgroud)
2)将其作为带有ansi格式的test.ansi保存在记事本中.
3)在记事本中将utf-8格式保存为test.utf8.
read.table("test.ansi",sep=",",header=TRUE) #can work fine
read.table("test.utf8",sep=",",header=TRUE) #can't work
Run Code Online (Sandbox Code Playgroud)
然后,我将编码设置为utf-8.
options(encoding="utf-8")
read.table("test.utf8",sep=",",header=TRUE,encoding="utf-8")
In read.table("test.utf8", sep = ",",header=TRUE,encoding = "utf-8") :
invalid input found on input connection 'test.utf8'
Run Code Online (Sandbox Code Playgroud)
如何读取数据文件(test.utf8)?
在python中,它非常简单
rfile=open("g:\\test.utf8","r",encoding="utf-8").read()
rfile
'\ufeff????,??,????\n\nHuangTianhui,?,1948/05/28\n\n???,?,1952/03
/27\n\n???,?,1994/12/09'
rfile.replace("\n\n","\n").replace("\ufeff","").splitlines()
['????,??,????', 'HuangTianhui,?,1948/05/28', '???,?,1952/03/27',
'???,?,1994/12/09']
Run Code Online (Sandbox Code Playgroud)
Python可以比R做得更好.
我像Sathish所说的那样,问题解决了一点,仍然保留了一些.
我发现当数据在data.frame中时,它无法正常显示,
当数据是一列data.frame时,它可以正常显示
,当数据是一行data.frame时,它就足够奇怪了,它无法正常显示.
