Apr-21-2017, 08:58 PM
I am trying to replace commas with a "^" in a string that is already quoted. Below you can see input and output. However my code does not replace the string.
Any help would be appreciated.
Input:
Any help would be appreciated.
Input:
Output:Id,Category,Description,Date
1,Test,Red Cars,02/12/2017
2,Test,Blue Cars,03/01/2017
3,Test,"Green, big cars",01/05/2016
Output should be:Output:Id,Category,Description,Date
1,Test,Red Cars,02/12/2017
2,Test,Blue Cars,03/01/2017
3,Test,"Green^ big cars",01/05/2016
import csv ifile = open('C:/Users/jpilon/Documents/test.csv', 'r') reader = csv.reader(ifile,delimiter=',') ofile = open('C:/Users/jpilon/Documents/test_new.csv', 'w') writer = csv.writer(ofile, delimiter=',') findlist = ['"*,*"'] replacelist = ['"*^*"'] rep = dict(zip(findlist, replacelist)) def findReplace(find, replace): s = ifile.read() s = s.replace(find, replace) ofile.write(s) for item in findlist: findReplace(item, rep[item]) ifile.close() ofile.close()