0
我有一些gzip文件是CSV文件。所以我没有使用csv
模块。如何在使用Python读取CSV文件时转义逗号?
某些字符字段封装在双引号内:"
,但不是全部。我的目标是读取行并基本上将数据复制到另一个文件。一些包含双引号的字段中包含逗号,而且我的脚本不能正确忽略引号内的逗号。我如何设置它,使Python忽略双引号内的字符?
这是有关这个问题的部分代码:
with gzip.open('gzippedfile.csv.gz', 'r') as campaign, open('output%s.csv' % today, 'wb') as output:
outputwriter = csv.writer(output, delimiter=',')
#Create variable 'count' to hold counter to skip reading the header line in the input file
count = 0
for line in campaign:
line=line.replace('\"','')
line=line.replace('\'','')
#print line
#Increment count by one each loop. This will make the loop skip the header line at the first iteration
count = count+1
if count == 1:
continue
#print today
#Create strings of the campaignid, whitelist entry, blacklist entry, and zipcode list each row
campaignid = line.split(',')[0].lstrip()
whitelist = line.split(',')[10].lstrip()
blacklist = line.split(',')[11]
zipcodes = line.split(',')[12]
我试图消除replace
8号线和9,但这并不解决问题。
哦,不知道我能做到这一点。是否有可能将每行读取为一个字符串?看起来'reader'将数据加载到字符串列表中。 – simplycoding
阅读器对象的全部要点是,一行中的字段被预解析为列表,其中列表索引表示列号,基于0。 –