且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用 R 转换文件编码?(ANSI 到 UTF-8)

更新时间:2023-11-27 19:15:16

你可以使用 iconv:

you can use iconv:

writeLines(iconv(readLines("tmp.html"), from = "ANSI_X3.4-1986", to = "UTF8"), "tmp2.html")

tmp2.html 应该是 utf-8.

tmp2.html should be utf-8.

由 Henrik 于 2015 年 6 月
从评论中提取的 Windows 工作解决方案如下:

Edit by Henrik in June 2015:
A working solution for Windows distilled from the comments is as follows:

writeLines(iconv(readLines("tmp.html"), from = "ANSI_X3.4-1986", to = "UTF8"), 
           file("tmp2.html", encoding="UTF-8"))

更新 2021:如果 ANSI 是当前语言环境,则以下方法也适用(即,使用本地编码作为 from 源):

Update 2021: And if ANSI is the current locale, the following works as well (i.e., uses the local encoding as from source):

writeLines(iconv(readLines("tmp.html"), from = "", to = "UTF8"), 
           file("tmp2.html", encoding="UTF-8"))