【发布时间】:2018-10-29 05:32:45
【问题描述】:
我正在尝试在 python 中使用 csv.reader 读取文件。我是 Python 新手,正在使用 Python 2.7.15。
我尝试重新创建的示例来自this 页面的“Reading CSV Files With csv”部分。这是代码:
import csv
with open('employee_birthday.txt') as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',')
line_count = 0
for row in csv_reader:
if line_count == 0:
print(f'Column names are {", ".join(row)}')
line_count += 1
else:
print(f'\t{row[0]} works in the {row[1]} department, and was born in {row[2]}.')
line_count += 1
print(f'Processed {line_count} lines.')
在执行代码期间,我收到以下错误:
File "ross_test2.py", line 11
print(f'Column names are {", ".join(row)}')
^
SyntaxError: invalid syntax
我做错了什么?我怎样才能避免这个错误。我将不胜感激。
【问题讨论】:
标签: python python-2.7 csv