仅使用read.table跳过非ASCII字符

时间:2017-07-18 08:33:14

标签: r csv encoding iconv read.table

我有一个非ASCII字符的csv文件。我只是想删除那些字符并阅读我的csv文件。

> tables <- lapply('/.././abc.csv', read.csv,header=F,stringsAsFactors=FALSE,fileEncoding="UTF-8")
Warning message:
In scan(file = file, what = what, sep = sep, quote = quote, dec = dec,  :
  invalid input found on input connection '/.././abc.csv'
> df= suppressWarnings(do.call(rbind, tables)) 

它没有读取完整的文件。它只读取非ASCII字符之前的记录。它在非ASCII字符后跳过了所有记录。

我不能使用iconv('/.././abc.csv', "latin1", "ASCII", sub=""),因为它期望x为vector。

cat '/.././abc.csv'
88036,120,151036.656250,2017-07-17 22:27:49,17-07-17 22:27:49
88036,120,151036.671875,2017-07-17 22:27:53,17-07-17 22:27:53
88036,310,151036.687500,2017-07-17 22:27:58,17-07-17 22:27:58
88036,310,151036.703▒▒F▒▒B▒▒▒D▒%▒▒▒2▒T▒▒K222642,17-07-17 22:28:03,2017-07-17 22:28:03
88036,310,151036.484375,2017-07-17 22:26:54,17-07-17 22:26:54
88036,310,151036.500000,2017-07-17 22:26:59,17-07-17 22:26:59

读取CSV文件后,它会跳过最后2条记录。任何帮助。

1 个答案:

答案 0 :(得分:0)

如果您先阅读然后再阅读

,该怎么办?

td <- td[,lapply(.SD,function(x){ iconv(x, "latin1", "ASCII", sub="")})]

假设您将csv文件读作data.table