为了账号安全,请及时绑定邮箱和手机立即绑定

read.csv中的无效多字节字符串

read.csv中的无效多字节字符串

白猪掌柜的 2019-10-31 14:12:41
我正在尝试导入日语的csv。这段代码:url <- 'http://www.mof.go.jp/international_policy/reference/itn_transactions_in_securities/week.csv'x <- read.csv(url, header=FALSE, stringsAsFactors=FALSE)返回以下错误:Error in type.convert(data[[i]], as.is = as.is[i], dec = dec, na.strings = character(0L)) : invalid multibyte string at '<91>ΊO<8b>y<82>ёΓ<e0><8f>،<94><94><84><94><83><8c>_<96>?̏?@(<8f>T<8e><9f><81>E<8e>w<92><e8><95>?@<8a>փx<81>[<83>X<81>j'我尝试更改编码(Encoding(url) <- 'UTF-8'也更改为latin1),并尝试删除read.csv参数,但在每种情况下均收到相同的“无效的多字节字符串”消息。是否应该使用其他编码,还是存在其他问题?
查看完整描述

3 回答

?
开满天机

TA贡献1786条经验 获得超12个赞

Encoding设置字符串的编码。它不会设置您想要的字符串表示的文件的编码。


经过尝试,这对我有用"UTF-8":


x <- read.csv(url, header=FALSE, stringsAsFactors=FALSE, fileEncoding="latin1")

您可能希望跳过前16行,并分别读入标题。无论哪种方式,仍有大量清理工作要做。


x <- read.csv(url, header=FALSE, stringsAsFactors=FALSE,

  fileEncoding="latin1", skip=16)

# get started with the clean-up

x[,1] <- gsub("\u0081|`", "", x[,1])    # get rid of odd characters

x[,-1] <- as.data.frame(lapply(x[,-1],  # convert to numbers

  function(d) type.convert(gsub(d, pattern=",", replace=""))))


查看完整回答
反对 回复 2019-10-31
  • 3 回答
  • 0 关注
  • 1128 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信