Reputation: 1
Trying to read a csv file and print contents:
with open('C:\test.csv') as csvfile:
csv_reader = csv.reader(csvfile, delimiter=',')
line_count = 0
for row in csv_reader:
if line_count == 0:
print(f'Column names are {", ".join(row[0])}')
line_count += 1
else:
print(f'\t{row[0]} works in the {row[1]} department in {row[2]}.')
I received following error:
SyntaxError: invalid syntax
PS C:\users\XXX\documents\python> python ReadCSV.py
File "ReadCSV.py", line 12
print(f'Column names are {", ".join(row[0])}')
^
SyntaxError: invalid syntax
Upvotes: 0
Views: 809
Reputation: 29580
Literal String Interpolation or "f-strings" was only introduced in Python3.6.
See:
https://www.python.org/dev/peps/pep-0498/
Your code works fine on Python3.6.
If you are not using Python3.6 (and up), you will get a syntax error.
$ python3.6 -V
Python 3.6.7
$ python3.6 readcsv.py
Column names are c, o, l, 1
1 works in the 2 department in 3.
4 works in the 5 department in 6.
$ python3 -V
Python 3.5.2
$ python3 readcsv.py
File "readcsv.py", line 9
print(f'Column names are {", ".join(row[0])}')
^
SyntaxError: invalid syntax
$ python -V
Python 2.7.12
$ python readcsv.py
File "readcsv.py", line 9
print(f'Column names are {", ".join(row[0])}')
^
SyntaxError: invalid syntax
Upvotes: 1