I need to do a find and replace (specific to one column of URLs) in a huge Excel .csv file. Since I’m in the beginning stages of trying to teach myself a scripting language, I figured I’d try to implement the solution in python.
I’m having trouble with the “replace” part of the solution. I’ve read the official csv module documentation about how to use the writer, but there isn’t really a clear enough example for me (yes, I’m slow). So, now for the question: how does one iterate through the rows of a csv file with a writer object?
p.s. apologies in advance for the clumsy code, I’m still learning 🙂
import csv
csvfile = open("PALTemplateData.csv")
csvout = open("PALTemplateDataOUT.csv")
dialect = csv.Sniffer().sniff(csvfile.read(1024))
csvfile.seek(0)
reader = csv.reader(csvfile, dialect)
writer = csv.writer(csvout, dialect)
total=0;
needchange=0;
changed = 0;
temp = ''
changeList = []
for row in reader:
total=total+1
temp = row[len(row)-1]
if '/?' in temp:
needchange=needchange+1;
changeList.append(row.index)
for row in writer: #this doesn't compile, hence the question
if row.index in changeList:
changed=changed+1
temp = row[len(row)-1]
temp.replace('/?', '?')
row[len(row)-1] = temp
writer.writerow(row)
print('Total URLs:', total)
print('Total URLs to change:', needchange)
print('Total URLs changed:', changed)
The reason you’re getting an error is that the writer doesn’t have data to iterate over. You’re supposed to give it the data – presumably, you’d have some sort of list or generator that produces the rows to write out.
I’d suggest just combining the two loops, like so:
And with that, you don’t even need
total,needchange, andchangeList. (There are a bunch of optimizations in there that I unfortunately don’t have time to explain, but I’ll see if I can edit that info in later)