0

Please advise how I'm getting the following error:

File "/Users/<username>/PycharmProjects/OOP/csv_write.py", line 8
  print(f'Column names are {", ".join(row)}')
                                           ^
SyntaxError: invalid syntax

Code:

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.')

As I'm trying to read from the file and display the information as follows as per the link.

employee_birthday.txt :

name,department,birthday month
John Smith,Accounting,November
Erica Meyers,IT,March

This question has been resolved and thank you for the responses below.

3
  • show and example for the data
    – Denis Tsoi
    Commented May 28, 2020 at 16:06
  • 2
    Please update your question with the full text of your code and full error traceback.
    – quamrana
    Commented May 28, 2020 at 16:08
  • Thanks, this is the code and sample data checked into the Git repository and it worked as suggested by @FishingCode: github.com/eugenesheyko/csv_write/blob/master/csv_write.py#L13 Appreciate if you can also advise how to print the 'line_count' variable and the names of the columns.
    – Eugene_S
    Commented May 28, 2020 at 17:07

2 Answers 2

1

Try this for that line the traceback is referring to:

f-strings apply for py3.5 and greater versions, see if either of these work:

print('Column names are',", ".join(row))

OR

print('Column names are %s'%", ".join(row))

OR

print('Column names are {}'.format(", ".join(row)))
2
1

Make sure you are using Python >= 3.6 as f-strings were introduced later. If that still doesn't work, check the indentation for the code.

0

Not the answer you're looking for? Browse other questions tagged or ask your own question.