我正在阅读包含瑞典字母åäö使用(readr)和(Laf)包的1 gb txt文件
my.data<- read_fwf('my.file', fwf_widths (c(2,2,2,8,2,4,40,1,1,10,10,4,2,11,32,1,4)),
progress = interactive())
获取价值而不是öäå
有什么建议吗?
答案 0 :(得分:0)
#Check what is your present encoding:
getOption("encoding")
#Modifiy the encoding
options(encoding = "ISO-8859-1")
#Optional: Check your system locale (Time, Monetary System, etc)
Sys.getlocale()
#Set an encoding, but it is OS-dependent what is installed
Sys.setlocale("LC_ALL", locale ="swe")
来自readr文档:“默认编码。这仅影响文件的读取方式 - 读取器始终将输出转换为UTF-8。”使用read_fwf
,您似乎可以指定区域设置/编码:
read_fwf(file, col_positions, col_types = NULL, locale = default_locale(),
na = c("", "NA"), comment = "", skip = 0, n_max = Inf,
guess_max = min(n_max, 1000), progress = show_progress())
将default_locale()
替换为locale(encoding= "latin1")
。您的示例不可再现,因此很难测试它是否有效,但它应该如下所示:
my.data<- read_fwf('my.file', fwf_widths (c(2,2,2,8,2,4,40,1,1,10,10,4,2,11,32,1,4)), locale= locale(encoding= "latin1"),
progress = interactive())