Reading csv file KeyError and passing to array

I have an example csv file named '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 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 works fine except for the last line, and has the following error message:

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

How could I avoid this?

+5
source share
2 answers

I cannot reproduce the problem using this cleaned 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 result that he 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 r2.csv tag, which I created myself and used, since you did not specify it:

 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 
0
source

I only worked with the top two lines of your csv, but this gives you the desired result:

 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 

And this returns:

 {'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
source

Source: https://habr.com/ru/post/1266883/


All Articles