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)