import csv
file = list(csv.reader(open('Books2.csv'))) # writes csv to a temporary list as csv files can't be altered
tmp = [] # calls new list tmp
for row in file: # This piece of code reads adds the data and adds it to the tmp list
tmp.append(row)
x = 0
for row in tmp: # for every row in the file
print(x,tmp[x]) # prints the data if in that row
x = x + 1 # moves one row down in the list
remove = int(input('Which row would you like to remove from the list?'))
del tmp[remove] # removes chosen entry from the list
x = 0
for row in file: # for every row in the file
print(x,tmp[x]) # prints the data if in that row
x = x + 1 # moves one row down in the list
change = int(input('Which row would you like to change?')
for row in file:
if tmp[z] == change:
book = input('Please enter the title of a book')
author = input('Please enter the name of the author')
year = input('Please enter the year the book was released')
new_entry = book + ',' + author + ',' + year + '\n' # creates new entry for the file
tmp[z] = str(new_entry)
file = open('Books2.csv','w') # opens a csv file which we are writing
y = 0
for row in tmp:
file.write(str(tmp[y]) + '\n')
y = y + 1
file.close
This code gives me the following error and I'm not sure why, can anyone offer any help or advice?
Error:
line 23
for row in file:
^
SyntaxError: invalid syntax
Thanks