I have the following code, and I simply want it to write the list to a file. It does so, but with a blank line each time, which causes problems with every iteration!
with open("films.txt", mode='r') as f:
allfilms=[]
reader = csv.reader(f)
for row in reader:
#create a list again which reads in and stores all films in the file
allfilms.append(row)
print(allfilms)
with open("films.txt","w") as f:
writer=csv.writer(f)
writer.writerows(allfilms)
The output is: 0,Genre, Title, Rating, Likes
1,Sci-Fi,Out of the Silent Planet, PG, 0
2,Sci-Fi,Solaris, PG,0
3,Sci-Fi,Star Trek, PG, 0
4,Sci-Fi,Cosmos, PG, 0
As mentioned, I don't want the blank line when saving the list. Also, I'd be interested in more elegant or better ways of doing the same thing. I want the file to be READ in to the list, and then written back to the file EXACTLY the same, in terms of formatting, as what was stored in the file.
Thanks in advance