我试图使用熊猫操作.csv文件,但我得到这个错误:

pandas.parser.CParserError:标记数据错误。C错误:第3行有2个字段,见12

我试着读过熊猫的文件,但一无所获。

我的代码很简单:

path = 'GOOG Key Ratios.csv'
#print(open(path).read())
data = pd.read_csv(path)

我该如何解决这个问题?我应该使用csv模块还是其他语言?

文件来自晨星公司


当前回答

对于这个问题,我遇到了多种解决方案。很多人也给出了最好的解释。但对于初学者来说,我认为以下两种方法就足够了:

import pandas as pd

#Method 1

data = pd.read_csv('file1.csv', error_bad_lines=False)
#Note that this will cause the offending lines to be skipped.

#Method 2 using sep

data = pd.read_csv('file1.csv', sep='\t')

其他回答

这看起来很丑,但你会有你的数据框架

import re
path = 'GOOG Key Ratios.csv'

try:
    data = pd.read_csv(path)
except Exception as e:
    val = re.findall('tokenizing.{1,100}\s*Expected\s*(\d{1,2})\s*',str(e),re.I)
    data = pd.read_csv(path, skiprows=int(val[0])-1)

我有一个类似的情况

train = pd.read_csv('input.csv' , encoding='latin1',engine='python') 

工作

你可以这样做,以避免问题-

train = pd.read_csv('/home/Project/output.csv' , header=None)

just add - header=None

希望这能有所帮助!!

对于这个问题,我遇到了多种解决方案。很多人也给出了最好的解释。但对于初学者来说,我认为以下两种方法就足够了:

import pandas as pd

#Method 1

data = pd.read_csv('file1.csv', error_bad_lines=False)
#Note that this will cause the offending lines to be skipped.

#Method 2 using sep

data = pd.read_csv('file1.csv', sep='\t')

在我的例子中,分隔符不是默认的“,”,而是Tab。

pd.read_csv(file_name.csv, sep='\\t',lineterminator='\\r', engine='python', header='infer')

注意:“\t”并不像某些来源所建议的那样有效。“\\t”是必需的。