Ghanshyam Savaliya
Ghanshyam Savaliya

Reputation: 618

Not able to read CSV file in python

I am trying to Import CSV file in python but I am getting following error while doing it.

import csv
with open('retlvl2.csv', 'rb') as csvfile:
    spamreader = csv.reader(csvfile, delimiter=' ', quotechar='|')
    for row in spamreader:
        print ', '.join(row)
  **File "<ipython-input-12-82caba3702b8>", line 4
    print ', '.join(row)
             ^
SyntaxError: invalid syntax**

Can anyone help on this, please?

Upvotes: 0

Views: 959

Answers (4)

nghiep truong
nghiep truong

Reputation: 310

I think the problem comes from your csv file. Lines can end in '\n', '\r', or '\r\n',.. You should try this

with open('C:\Test\example.csv', newline='') as csvfile:

with open('C:\Test\example.csv', newline='\n') as csvfile:

...

I am not sure but you should try.

Upvotes: 0

Pabasara Ranathunga
Pabasara Ranathunga

Reputation: 170

Try with parenthesis, What is your python version???

print (', '.join(row))

Upvotes: 0

Lauro Bravar
Lauro Bravar

Reputation: 373

If you are using Python 3 you should add parenthesis to your print declaration:

print(', '.join(row))

Upvotes: 2

Daniel McLaury
Daniel McLaury

Reputation: 4293

Probably you're trying to use python2 syntax in python3. print needs parentheses in python3.

Upvotes: 2

Related Questions