Reputation: 258
I am currently having trouble out a method to save the results in the file I provided through my sys.argv[1]. I am providing a csv to the python script.
My csv has data in the format like this
3/4/20
3/5/20
3/6/20
I have tried using append() but I am receiving errors, I also attempted using write()
import sys
file = open(str(sys.argv[1])) #enter csv path name, make sure the file only contains the dates
for i in file:
addedstring = (i.rstrip() +',09,00, 17')
finalstring = addedstring.replace("20,", "2020,")
file.append(i)
Any help is greatly appreciated!
Upvotes: 3
Views: 78
Reputation: 13533
One option is to put the modified strings into a list, then close the file, re-open for writing, and write the list of modified strings:
finalstring = []
with open(sys.argv[1], "r") as file:
for i in file:
addedstring = (i.rstrip() +',09,00, 17')
finalstring.append(addedstring.replace('20,', '2020,'))
with open(sys.argv[1], "w") as file:
file.write('\n'.join(finalstring))
Upvotes: 5