python - KeyError读取csv文件并传输到数组

原文 标签 python arrays csv

KeyError reading csv file and transferring to array

I have an example csv file with name '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
-------------------------------------------------------------------------

I have the following python code to read a csv file and transfer the columns to arrays:

# 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)

It runs fine except for the last row and has the following error message:

File "<stdin>", line 1, in <module>
  KeyError: 'Room_Rev'

How could I avoid it?

Answer

I am unable to reproduce the problem using this cleaned-up version of your code:

# 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)

This is the printed output it produced:

 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']}

And this the r2.csv test I created myself and used since you didn't provide one:

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

翻译

我有一个名为'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'


我该如何避免呢?
最佳答案
我无法使用此清理后的代码版本来重现该问题:

# 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
相关推荐

python - Doc2Vec句子聚类

python - Scrapy python csv输出的每一行之间都有空白行

python - 如何检测填充了哪些圆圈+ OpenCV + Python

python - 是否可以更改当地人的命令?

python - 为Python2和Python3编写Unicode正则表达式

python - pip安装tesserocr失败,并显示错误“ tesserocr的建筑轮失败”

python - webbrowser python在同一选项卡中打开新的URL。这行得通吗?

python - 使用Python进行轨迹聚类/聚合

python - 使用model.fit_generator时keras val非常慢

python - 在Python 2.7中模拟布尔__bool__协议方法