使用Python读取CSV文件 [英] Reading a CSV file using Python

查看:302
本文介绍了使用Python读取CSV文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请告诉我这个代码有什么问题,它给出一个错误

  import csv 
with open some csv','rb')as f:
reader = csv.reader(f)
读取行:
print row

解决方案

您使用的是哪个版本的Python?



with 语句在2.6中是新的 - 如果你使用2.5,你需要从__future__ import with_statement 中得到。如果你使用的Python比2.5大,那么没有语句,所以只需写:

  import csv 
f = open('some.csv','rb')
reader = csv.reader(f)
读取器中的行:
print row
f.close()

更好的是更新到现代版本的Python ,但。 Python 2.5几乎在5年前发布,2.x行中的当前版本是2.7


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 row in reader:
        print row

解决方案

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

这篇关于使用Python读取CSV文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆