简体   繁体   中英

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?

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

如果您使用的是Python 3,则应在打印声明中添加括号:

print(', '.join(row))

尝试使用括号,您的python版本是什么???

print (', '.join(row))

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM