Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Unexpected syntax error
#1
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
Reply
#2
Please show more code, the error is most likely because of the previous line
Reply
#3
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?
Reply
#4
The previous line has a ) missing off the end
Reply
#5
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
Reply
#6
Help was offered yesterday by @Yoriz
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Syntax error for "root = Tk()" dlwaddel 15 1,007 Jan-29-2024, 12:07 AM
Last Post: dlwaddel
Photo SYNTAX ERROR Yannko 3 333 Jan-19-2024, 01:20 PM
Last Post: rob101
  Syntax error while executing the Python code in Linux DivAsh 8 1,450 Jul-19-2023, 06:27 PM
Last Post: Lahearle
  Code is returning the incorrect values. syntax error 007sonic 6 1,135 Jun-19-2023, 03:35 AM
Last Post: 007sonic
  syntax error question - string mgallotti 5 1,248 Feb-03-2023, 05:10 PM
Last Post: mgallotti
  Syntax error? I don't see it KenHorse 4 1,194 Jan-15-2023, 07:49 PM
Last Post: Gribouillis
  Syntax error tibbj001 2 847 Dec-05-2022, 06:38 PM
Last Post: deanhystad
  Python-for-Android:p4a: syntax error in main.py while compiling apk jttolleson 2 1,777 Sep-17-2022, 04:09 AM
Last Post: jttolleson
  Mysql Syntax error in pymysql ilknurg 4 2,287 May-18-2022, 06:50 AM
Last Post: ibreeden
  Solving equation equal to zero: How to resolve the syntax error? alexfrol86 3 1,894 Feb-21-2022, 08:58 AM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020