Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将格式不正确的CSV读入熊猫-未替换的引号_Python_Csv_Pandas - Fatal编程技术网

Python 将格式不正确的CSV读入熊猫-未替换的引号

Python 将格式不正确的CSV读入熊猫-未替换的引号,python,csv,pandas,Python,Csv,Pandas,我继承了几百个CSV,我想导入熊猫数据帧。它们的格式如下所示: username;date;retweets;favorites;text;geo;mentions;hashtags;id;permalink ;2011-03-02 11:04;0;0;"ICYMI: "What you have is 87 people who have common goals of working for [the] next generation; that’s why our...";;;;"4299

我继承了几百个CSV,我想导入熊猫数据帧。它们的格式如下所示:

username;date;retweets;favorites;text;geo;mentions;hashtags;id;permalink
;2011-03-02 11:04;0;0;"ICYMI: "What you have is 87 people who have common goals of working for [the] next generation; that’s why our...";;;;"42993734165594112";https://twitter.com/AustinScottGA08/status/42993734165594112
;2014-02-25 10:38;3;0;"Will be asking tough questions of #IRS at 2/26 FSGG hearing; supporting bills to make agency more accountable.";;;#IRS;"438352361812426752";https://twitter.com/AnderCrenshaw/status/438352361812426752
;2017-06-14 12:39;4;6;"Thank you to the brave men and women who have answered the call to defend our great nation. Happy 242nd Birthday @USArmy ! #ArmyBDay pic.twitter.com/brBYCOLBJZ";;@USArmy;#ArmyBDay;"875045042758369281";https://twitter.com/AustinScottGA08/status/875045042758369281
要将其拉入熊猫数据帧,我尝试:

tweets=pd.read\u csv(文件,头=0,sep=';',解析日期=True)

得到了这个错误:

ParserError:标记数据时出错。C错误:第1行预期有10个字段,saw 11

我想这是因为字段中有一个未替换的引用

伊克米:“你们有87个人,他们都有为下一代工作的共同目标;这就是为什么我们的

所以,我试过了

tweets=pd.read_csv(文件,头=0,sep=';',解析_dates=True,quoting=csv.QUOTE_NONE)

并获得一个新错误(我假设,因为字段中有):

将在2/26 FSGG听证会上向IRS提出棘手问题; 支持法案,使机构更加负责。http:// tinyurl.com/n8ozeg5

ParserError:标记数据时出错。C错误:第2行中预期有10个字段,saw 11


我无法重新生成这些CSV文件。我想知道的是,我如何预处理/修复它们,使其格式正确(即字段中的转义引号)或者,有没有一种方法可以直接将它们读入数据框,即使是使用未替换的引号?

我会在读入熊猫之前清理数据。以下是我对您当前问题的解决方案。

编辑:
这将替换双引号内的
(基于答案)

原件:

o = open("fileOut.csv", 'w')
with open("fileIn.txt") as f:
    for lines in f:
        o.write(lines.replace("; ", ""))
o.close()

你使用的是什么版本的python和pandas?我在python 3.6.1和pandas 0.19.2Python 3.5.3 pandas 0.20.2上得到了不同的结果-对你来说会发生什么?在这种情况下,我不需要每一列,添加
usecols
修复了我眼前的问题。但它并没有回答我的实际问题。下面是行代码:
tweets=pd.read_csv(file,header=0,sep=';',parse_dates=True,quoting=csv.QUOTE_NONE,usecols=[“date”,“hashtags”,“permalink”])
tweets中的;后面不总是跟空格,所以这只适用于某些情况。例如
;2013-07-15 15:35;1;0@国会照片第15天-美丽的东西:从美国国会大厦的演讲者阳台上看;。。。http://fb.me/2ZHDzR8XQ;@Congressionalphotoday;;“356874563839201280”;https://twitter.com/AustinScottGA08/status/356874563839201280
@Libby:在这种情况下,请使用类似正则表达式的正则表达式。
re.sub(“\”[^]*\”,lambda x:x.group(0)。replace(“;”,“\;”),line)
将在引号内替换
o = open("fileOut.csv", 'w')
with open("fileIn.txt") as f:
    for lines in f:
        o.write(lines.replace("; ", ""))
o.close()