R write.csv使用UTF-16编码 [英] R write.csv with UTF-16 encoding

查看:222
本文介绍了R write.csv使用UTF-16编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法使用 write.csv 使用UTF-16字符编码来输出data.frame。

I'm having trouble outputting a data.frame using write.csv using UTF-16 character encoding.

背景:我想从data.frame中写出一个CSV文件,以便在Excel中使用。 Excel Mac 2011似乎不喜欢UTF-8(如果我在文本导入期间指定UTF-8,非ASCII字符显示为下划线)。我已经相信Excel会很高兴与UTF-16LE编码。

Background: I am trying to write out a CSV file from a data.frame for use in Excel. Excel Mac 2011 seems to dislike UTF-8 (if I specify UTF-8 during text import, non-ASCII characters show up as underscores). I've been led to believe that Excel will be happy with UTF-16LE encoding.

这里是示例data.frame:

Here's the example data.frame:

> foo
  a  b
1 á 羽
> Encoding(levels(foo$a))
[1] "UTF-8"
> Encoding(levels(foo$b))
[1] "UTF-8"

所以我试图输出data.frame通过做:

So I tried to output the data.frame by doing:

f <- file("foo.csv", encoding="UTF-16LE")
write.csv(foo, f)

一个ASCII文件,格式如下:

This gives me an ASCII file that looks like:

"","

如果我使用 encoding =UTF-16,我得到一个只包含字节顺序标记 0xFE 0xFF

If I use encoding="UTF-16", I get a file that only contains the byte-order mark 0xFE 0xFF.

如果使用 encoding =UTF-16BE,我得到一个空文件。

If I use encoding="UTF-16BE", I get an empty file.

这是在64位版本的R 2.12.2在Mac OS X 10.6.6。

This is on a 64-bit version of R 2.12.2 on Mac OS X 10.6.6. What am I doing wrong?

推荐答案

您可以直接在UTF-8中保存 csv ,然后将其转换为在终端中使用 iconv 的UTF-16LE。

You could simply save the csv in UTF-8 and later convert it to UTF-16LE with iconv in terminal.

如果你坚持在R中做,以下可能工作 - 虽然看起来 iconv 在R有一些问题,请参阅: http://tolstoy.newcastle.edu.au/R/e10/devel/10/06/0648.html

If you insist on doing it in R, the following might work - althought it seems that iconv in R does have some issues, see: http://tolstoy.newcastle.edu.au/R/e10/devel/10/06/0648.html

> x <- c("foo", "bar")
> iconv(x,"UTF-8","UTF-16LE")
Error in iconv(x, "UTF-8", "UTF-16LE") : 
  embedded nul in string: 'f\0o\0o\0'

正如你可以看到,上面的链接补丁真的需要 - 我没有测试,但如果你想保持它simly(和讨厌):只是调用第三方iconv程序里面R与系统调用保存表到 csv

As you can see the above linked patch is really needed - which I did not tested, but if you want to keep it simly (and nasty): just call the third party iconv program inside R with a system call after saving the table to csv.

这篇关于R write.csv使用UTF-16编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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