[英]a bug with csv module in python
import csv
with open('database.csv') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
print(row['NAME'])
Guys, I have a csv file with the first row as a index, and in linux the code read from row['NAME'] and print only the names form colum NAME, when I run it in windows, it says: 伙计们,我有一个csv文件,其中第一行作为索引,在linux中,代码是从row ['NAME']读取的,并且仅在colum NAME中打印名称,当我在Windows中运行它时,它说:
C:\Users\Desktop>python py.py
Traceback (most recent call last):
File "py.py", line 5, in <module>
print(row['NAME'])
KeyError: 'NAME'
WHY? 为什么?
If you are using the python 2- versions, you need to open the csv with rb, ie: 如果使用的是Python 2-版本,则需要使用rb打开csv,即:
with open('database.csv', 'rb') as csvfile:....
for reference, checkout https://docs.python.org/2/library/csv.html as it includes a part in the reader doc about this. 作为参考,请检阅https://docs.python.org/2/library/csv.html,因为它在读者文档中包含了有关此内容的部分。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.