Python Forum

Full Version: Unexpected syntax error
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Error:
for row in file: ^ SyntaxError: invalid syntax
I keep getting this error on my code I've used this line of code on other programs without issue before. Can anyone tell me why I'm getting this error/ how to fix it? The arrow is pointed at the colon.

Thanks
Please show more code, the error is most likely because of the previous line
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:
Hi here's more of the code could you offer any advice based on this?
The previous line has a ) missing off the end
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
Help was offered yesterday by @Yoriz