5

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?

2
  • At first glance, shouldn't it be comma separated values instead of pipes? Commented Apr 18, 2017 at 21:23
  • @GarrettKadillak do you mean the csv file? It is comma separated value file. Commented Apr 18, 2017 at 21:29

2 Answers 2

1

I worked off just the top two rows of your csv but this gives you the desired output:

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']}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you @Dmitry Polonskiy I tried the code but it returns same error message as I posted.
Thank you! Error from input data of my side
0

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

2 Comments

thank you @martineau With your check information, I found I input one more 'space' in the column name 'Room_rev'. And now done.
Chenxi: You're welcome. Although sometimes ridiculed, just printing things at certain critical points in your code is a very old—and underrated—debugging technique. It's also especially suited for Python since there's no prolonged code compiling and linking steps to go through first.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.