Reputation: 79
i have a syntax error with the with statement. i cant figure it out. i am using python 3 and every time i use the with statement it just wont have it. i am trying to make a reader for a txt file and i need this with statement.
import csv
value1 = "Spam"
fname = input(open("Please enter the name of the file you wish to open: ")
with open(fname, 'rb') as csvfile:
spamreader = csv.reader(csvfile, delimiter=' ', quotechar='|')
for row in spamreader:
if value1 in row:
print(" ".join(row))
i am very new to python its probably very obvious cheers for the help guys
Upvotes: 3
Views: 2411
Reputation: 369244
The syntax error is not caused by with
, but print
statement. In Python 3.x, print
is a function.
>>> print 1
File "<stdin>", line 1
print 1
^
SyntaxError: invalid syntax
>>> print(1)
1
>>>
So the following line:
print " ".join(row)
should be replaced with:
print(" ".join(row))
In the following sentence, a parenthesis is missing.
fname = input(open("Please enter the name of the file you wish to open: "))
# ^
And open
should be omitted:
fname = input("Please enter the name of the file you wish to open: ")
Upvotes: 4