[英]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'])
伙計們,我有一個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'
為什么?
如果使用的是Python 2-版本,則需要使用rb打開csv,即:
with open('database.csv', 'rb') as csvfile:....
作為參考,請檢閱https://docs.python.org/2/library/csv.html,因為它在讀者文檔中包含了有關此內容的部分。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.