简体   繁体   English

在 R 中读取 csv 文件时出错

[英]Error while reading csv file in R

I am having some problems in reading a csv file with R.我在用 R 读取 csv 文件时遇到了一些问题。

 x=read.csv("LorenzoFerrone.csv",header=T)

Error in make.names(col.names, unique = TRUE) : 
      invalid multibyte string at '<ff><fe>N'

I can read the file using libre office with no problems.我可以使用 libre office 读取文件,没有问题。

I can not upload the file because it is full of sensible information.我无法上传文件,因为它充满了合理的信息。

What can I do?我能做什么?


Setting encoding seem like the solution to the problem.设置编码似乎是解决问题的方法。

> x=read.csv("LorenzoFerrone.csv",fileEncoding = "UCS-2LE")
> x[2,1]
[1] Adriano Caruso
100 Levels:  Ada Adriano Caruso adriano diaz Adriano Diaz alberto ferrone Alexey ... Zia Tina

The cause is an invalid encoding.原因是无效的编码。 I have solved replacing all the "è" with e我已经解决了用 e 替换所有的“è”

This will read the column names as-is and won't return any errors:这将按原样读取列名,并且不会返回任何错误:

x = read.csv(check.names = F)

To remove/replace troublesome characters in column names, use this:要删除/替换列名称中的麻烦字符,请使用以下命令:

iconv(names(x), to = "ASCII", sub = "")

我发现这个问题是由文件代码引起的,我解决了这个问题,用Windows note打开它,用UTF-8保存,然后用Excel重新打开(一开始是乱码),然后用UTF-8重新保存,然后就可以了!

您需要在sep参数中指定正确的分隔符。

You can always use the "Latin1" encoding while reading the csv:您可以在阅读 csv 时始终使用“Latin1”编码:

 x = read.csv("LorenzoFerrone.csv", fileEncoding = "Latin1", check.names = F)

I am adding check.names = F to avoid replacing spaces by dots within your header.我正在添加check.names = F以避免在标题中用点替换空格。

Typically an encoding issue.通常是编码问题。 You can try to change encoding or else deleting the offending character (just use your favorite editor and replace all instances).您可以尝试更改编码或删除有问题的字符(只需使用您喜欢的编辑器并替换所有实例)。 In some cases R will spit the char location, for example:在某些情况下,R 会吐出字符位置,例如:

invalid multibyte string 1847无效的多字节字符串 1847

Which should make your life easier.这应该会让你的生活更轻松。 Also note that you may be required to repeat this process several times (deleting all offending characters or trying several encodings).另请注意,您可能需要多次重复此过程(删除所有有问题的字符或尝试多种编码)。

Change the file format to - CSV UTF-8.将文件格式更改为 - CSV UTF-8。 It worked for me.它对我有用。

不确定这是否有帮助,但我遇到了类似的问题,并发现这是因为我的“csv”文件有一个 .csv 后缀,但实际上是一个 .xls 文件!

不确定这是否有帮助,只是有一个类似的问题,我通过从我尝试导入的 csv 中删除“来解决这个问题。数据库的第一行将列名写为“colname”、“colname2”、“etc”和我删除了所有 " 然后在 R 中读取了 csv 就好了。

I solved the problem by removing any graphical signs in the writing (ie accent marks).我通过删除文字中的任何图形符号(即重音符号)解决了这个问题。 My headers were written in Spanish and had some accent marks in there.我的标题是用西班牙语写的,里面有一些重音符号。 I replaced with simple words (México=Mexico) and problem was solved.我用简单的单词(México=Mexico)代替,问题就解决了。

我知道这是一个旧帖子,但只是想对非英语本地人说,如果您使用“,”作为十进制分隔符,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM