KeyError读取csv文件并转移到数组

时间:2017-04-18 21:11:42

标签: python arrays csv

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

我怎么能避免它?

2 个答案:

答案 0 :(得分: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']}

答案 1 :(得分:0)

我无法使用此代码的清理版本重现问题:

# 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