Reputation: 2967
I have some gzip files that are CSV files. So I'm not using the csv
module.
Some character fields are encapsulated within double quotes: "
, but not all of them. My goal is to read the lines and basically copy the data to another file. Some of the fields that contain the double quotes have commas in them, and my script isn't properly ignoring commas within the quotes. How do I set it so Python ignores characters within double quotes?
This is part of the code pertaining to the question:
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]
I've tried removing the replace
lines 8 and 9 but that doesn't fix the problem.
Upvotes: 0
Views: 1842
Reputation: 6893
Why don't you use csv.reader
with the file handle from gzip.open
?
with gzip.open('gzippedfile.csv.gz', 'r') as campaign, open('output%s.csv' % today, 'wb') as output:
reader = csv.reader(campaign) # look ma' no manual escaping
outputwriter = csv.writer(output, delimiter=',')
Upvotes: 4