Fixing a CSV using Python -


i trying clean formating on csv file in order import database, , i'm using following edit it:

f1 = open('visit_summary.csv', 'r') f2 = open('clinics.csv', 'w')  line in f1:     f2.write(line.replace('calendar: ', ''))  f1.close() f2.close() 

this works fine if there 1 edit make, however, have repeat code 19 times in order make changes required; opening , closing each file several times , having multiple placeholder fiels in order use intermediate steps ebtween first , last edit). there simpler way this? tried adding more "f2.write(line.replace"... lines, however, creates final file duplicated lines each of has 1 edit. think see problem (i writing each line multiple times each edit), however, cannot seem find solution. new python , self teachign myself help, or direction better resources appreciated.

there's no reason can't lots of things line before write it:

with open('visit_summary.csv', 'r') f1, open('clinics.csv', 'w') f2:     line in f1:         line = line.replace('calendar: ', '')         line = line.replace('something else', '')         f2.write(line) 

(i replaced open, close with statement)


Comments

Popular posts from this blog

java - Jmockit String final length method mocking Issue -

asp.net - Razor Page Hosted on IIS 6 Fails Every Morning -

c++ - wxwidget compiling on windows command prompt -