Reading a CSV file using Python

前端 未结 2 416
隐瞒了意图╮
隐瞒了意图╮ 2020-12-09 11:59

please tell me what\'s the problem in this code it\'s giving an error

import csv
with open(\'some.csv\', \'rb\') as f:
    reader = csv.reader(f)
    for ro         


        
相关标签:
2条回答
  • 2020-12-09 12:29
    from __future__ import with_statement
    

    And if that doesn't work, rewrite it to not use with in the first place.

    0 讨论(0)
  • 2020-12-09 12:37

    Which version of Python are you using?

    The with statement is new in 2.6 - if you're using 2.5 you need from __future__ import with_statement. If you use a Python older than 2.5 then there's no with statement, so just write:

    import csv
    f = open('some.csv', 'rb')
    reader = csv.reader(f)
    for row in reader:
        print row
    f.close()
    

    It's really better to update to a modern version of Python, though. Python 2.5 was released almost 5 years ago, and the current version in the 2.x line is 2.7

    0 讨论(0)
提交回复
热议问题