KeyError 读取 csv 文件并传输到数组
Posted
技术标签:
【中文标题】KeyError 读取 csv 文件并传输到数组【英文标题】:KeyError reading csv file and transferring to array 【发布时间】:2017-09-14 21:19:35 【问题描述】:我有一个名为 'r2.csv'
的示例 csv 文件:
Factory | Product_Number | Date | Avg_Noshow | Walk_Cost | Room_Rev
-------------------------------------------------------------------------
A | 1 | 01APR2017 | 5.6 | 125 | 275
-------------------------------------------------------------------------
A | 1 | 02APR2017 | 4.5 | 200 | 300
-------------------------------------------------------------------------
A | 1 | 03APR2017 | 6.6 | 150 | 250
-------------------------------------------------------------------------
A | 1 | 04APR2017 | 7.5 | 175 | 325
-------------------------------------------------------------------------
我有以下 python 代码来读取 csv 文件并将列传输到数组:
# Read csv file
import csv
with open('r2.csv', 'r') as infile:
reader = csv.DictReader(infile)
data =
for row in reader:
for header, value in row.items():
try:
data[header].append(value)
except KeyError:
data[header] = [value]
# Transfer the column from list to arrays for later computation.
mu = data['Avg_Noshow']
cs = data['Walk_Cost']
co = data['Room_Rev']
mu = map(float,mu)
cs = map(float,cs)
co = map(float,co)
除了最后一行之外,它运行良好,并且有以下错误消息:
File "<stdin>", line 1, in <module>
KeyError: 'Room_Rev'
我该如何避免呢?
【问题讨论】:
乍一看,不应该是逗号分隔值而不是管道吗? @GarrettKadillak 你的意思是 csv 文件吗?它是逗号分隔值文件。 【参考方案1】:我只处理了 csv 的前两行,但这会为您提供所需的输出:
with open('r2.csv', 'rb') as fin:
reader = csv.DictReader(fin)
data =
for row in reader:
for k, v in row.iteritems():
if k in data:
data[k] = [data[k],v]
else:
data[k] = v
这会返回:
'Avg_Noshow': ['5.6', '4.5'],
'Date': ['1-Apr-17', '2-Apr-17'],
'Factory': ['A', 'A'],
'Product_Number': ['1', '1'],
'Room_Rev': ['275', '300'],
'Walk_Cost': ['125', '200']
【讨论】:
谢谢@Dmitry Polonskiy 我尝试了代码,但它返回的错误消息与我发布的相同。 谢谢!我这边输入数据出错【参考方案2】:我无法使用您的代码的清理版本重现问题:
# Read csv file
import csv
with open('r2.csv', 'r') as infile:
reader = csv.DictReader(infile)
data =
for row in reader:
print('row: '.format(row))
for header, value in row.items():
try:
data[header].append(value)
except KeyError:
data[header] = [value]
print('')
from pprint import pprint
pprint(data)
# Transfer the column from list to arrays for later computation.
mu = data['Avg_Noshow']
cs = data['Walk_Cost']
co = data['Room_Rev']
mu = map(float, mu)
cs = map(float, cs)
co = map(float, co)
这是它产生的打印输出:
row: 'Walk_Cost': '125', 'Factory': 'A', 'Avg_Noshow': '5.6', 'Product_Number': '1', 'Date': '01APR2017', 'Room_Rev': '275'
row: 'Walk_Cost': '200', 'Factory': 'A', 'Avg_Noshow': '4.5', 'Product_Number': '1', 'Date': '02APR2017', 'Room_Rev': '300'
row: 'Walk_Cost': '150', 'Factory': 'A', 'Avg_Noshow': '6.6', 'Product_Number': '1', 'Date': '03APR2017', 'Room_Rev': '250'
row: 'Walk_Cost': '175', 'Factory': 'A', 'Avg_Noshow': '7.5', 'Product_Number': '1', 'Date': '04APR2017', 'Room_Rev': '325'
'Avg_Noshow': ['5.6', '4.5', '6.6', '7.5'],
'Date': ['01APR2017', '02APR2017', '03APR2017', '04APR2017'],
'Factory': ['A', 'A', 'A', 'A'],
'Product_Number': ['1', '1', '1', '1'],
'Room_Rev': ['275', '300', '250', '325'],
'Walk_Cost': ['125', '200', '150', '175']
这是我自己创建并使用的r2.csv
测试,因为您没有提供:
Factory,Product_Number,Date,Avg_Noshow,Walk_Cost,Room_Rev
A,1,01APR2017,5.6,125,275
A,1,02APR2017,4.5,200,300
A,1,03APR2017,6.6,150,250
A,1,04APR2017,7.5,175,325
【讨论】:
谢谢@martineau 根据您的检查信息,我发现我在列名“Room_rev”中又输入了一个“空格”。现在完成了。 晨曦:不客气。尽管有时会受到嘲笑,但仅在代码中的某些关键点打印内容是一种非常古老且被低估的调试技术。它也特别适用于 Python,因为不需要先进行长时间的代码编译和链接步骤。以上是关于KeyError 读取 csv 文件并传输到数组的主要内容,如果未能解决你的问题,请参考以下文章
Python 中使用朴素贝叶斯进行简单文本分类 - KeyError