I've got a books.txt
file which contains book titles, authors and prices as follows:
The Hunger Games,Suzanne Collins,12.97
The Fault In Our Stars,John Green,11.76
The Notebook,Nicholas Sparks,11.39
I sorted it into a list of lists to get this:
[[The Hunger Games, Suzanne Collins, 12.97], [The Fault In Our Stars, John Green, 11.76], [The Notebook, Nicholas Sparks, 11.39]]
The code I used is:
def booksDatabase():
for line in infile:
line = line.rstrip().split(",")
line[2] = float(line[2])
table.append(line)
infile = open("books.txt")
table = []
booksDatabase()
infile.close()
And I'd like to update the .txt
file so that it contains the current list of lists. How do I do that without importing any libraries?
Thanks in advance.
Update: I tried doing this:
def booksDatabase():
for line in infile:
line = line.rstrip().split(",")
line[2] = float(line[2])
table.append(line)
outfile.write(line)
infile = open("books.txt")
outfile = open("books.txt", 'w')
table = []
booksDatabase()
infile.close()
But I got this error:
outfile.write(line)
TypeError: write() argument must be str, not list
What am I doing wrong?