0

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?

2
  • 1
    The **File "<ipython-input-12-82caba3702b8>", line 4, is indeed invalid syntax. It looks like something copied from an error message? Commented Apr 2, 2018 at 7:26
  • 2
    What version of Python are you using? Commented Apr 2, 2018 at 7:26

4 Answers 4

3

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

print(', '.join(row))
Sign up to request clarification or add additional context in comments.

2 Comments

Thak you for delete answer :)+30
thanksss I just had the same problem
2

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

Comments

1

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

print (', '.join(row))

3 Comments

Thanks for the input. its Python 3.6.4 32 Bits.
but even after that, I am getting an error with open('retlvl2.csv', 'rb') as csvfile: spamreader = csv.reader(csvfile, delimiter=' ', quotechar='|') for row in spamreader: print (', '.join(row)) Traceback (most recent call last): File "<ipython-input-13-cd38e3cb57f8>", line 3, in <module> for row in spamreader: Error: iterator should return strings, not bytes (did you open the file in text mode?)
you have opened the file in 'rb' [read the file in binary] mode. Replace 'rb' with 'rt', it will work fine :-)
0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.