0

I'm really stuck on some code. Been working on it for a while now, so any guidance is appreciated. I'm trying to map many files related columns into one final column. The logic of my code will start by

  1. identifying my desired final column names
  2. reading the incoming file
  3. identifying the top rows as my column headers/names
  4. identifying all underneath rows as data for that column
  5. based on the column header, add data from that column to the most closely related column, and
  6. have an exit condition (if no more data, end program).

If anyone could help me with steps 3 and 4, I would greatly appreciate it because that is where I'm currently stuck.

It says I have a KeyError:0 where it says columnHeader=row[i]. Does anyone know how to solve this particular problem?

#!/usr/bin/env python
import sys #used for passing in the argument
import csv, glob

SLSDictionary={}

fieldMap = {'zipcode':['Zip5', 'zip4'],
        'firstname':[],
        'lastname':[],
        'cust_no':[],
        'user_name':[],
        'status':[],
        'cancel_date':[],
        'reject_date':[],
        'streetaddr':['address2', 'servaddr'],
        'city':[],
        'state':[],
        'phone_home':['phone_work'],
        'email':[]
        }

CSVreader = csv.DictReader(open( 'N:/Individual Files/Jerry/2013 customer list qc, cr, db, gb 9-19-2013_JerrysMessingWithVersion.csv', "rb"),dialect='excel', delimiter=',')

i=0
for row in CSVreader:
        if i==0:
            columnHeader = row[i]
        else:
            columnData = row[i]
        i += 1
Stefan van den Akker
  • 6,242
  • 7
  • 43
  • 62
  • You're using the DictReader, which does steps 3 and 4 for you automatically, as described in the documentation. What is your question exactly? – Wander Nauta Jun 02 '14 at 18:05
  • Also, you are not using either your SLSDictionary or your fieldMap - are you sure you've pasted the complete code? – Wander Nauta Jun 02 '14 at 18:06
  • i maybe (probably am) wrong, but I think I only get the first value as my value for my keys. As opposed to the hundreds of values I'd like to have associated with each key.And i suppose the empty SLSDictionary is not needed.And i havent finish steps 5 or 6 yet, so that's y i haven't used fieldMap yet, but believe me, I'd like to :) My main issue is that I can't step through each row and assign the data to my key's values. – user3692506 Jun 02 '14 at 18:09
  • Can you try replacing the contents of your loop with `print row`, just so you can see how the `DictReader` works? – Wander Nauta Jun 02 '14 at 18:12
  • Are you sure you want to use a dict reader? It seems like you are trying to access it as if it were a `csv.reader`. – nwalsh Jun 02 '14 at 18:15
  • Yes. Still decyphering what the result mean. Here's a very small portion of the output: {'': '', 'paytype_mc': 'TRUE', 'dpbc': '', 'cust_no': '126', 'phone_fax': '', 'custsize': '0', 'city': 'Bettendorf', 'container': '0', 'seq': '0', 'title': 'Dr.', 'paytype_am': 'TRUE', 'zipcode': '52722-5026', 'state': 'IA', 'phone_othr': '', 'latitude': '41.52724', 'company': '', 'email': '', 'status': '0', 'telemarket': 'FALSE', – user3692506 Jun 02 '14 at 18:15
  • Is there a better approach to accessing csv.dictReader? I've tried with csv.reader, and it makes steps 5 & 6 harder for me to code – user3692506 Jun 02 '14 at 18:17
  • To wander nauta, I believe my output is a series of 1 key to 1 value pairs. I need 1 key to many value pairs. Thanks for everything so far though my friends – user3692506 Jun 02 '14 at 18:19
  • Ah! I think I get what you want to do. You want to turn a list of dicts (a list of mappings from column name to column value; your input rows) into a dict of lists (a mapping from a column name to a list of column values). Here's how you could do that: http://stackoverflow.com/a/5559178/182402 Although I'm not sure why you'd want to... – Wander Nauta Jun 02 '14 at 18:22
  • I'm an new intern learning python, and they want me to manipulate some incoming data from multiple CSV files. Hopefully that explains my motive. Although, sometimes I like to code for fun lol :) – user3692506 Jun 02 '14 at 18:25
  • And thank you everyone for your help!!! – user3692506 Jun 02 '14 at 18:42
  • Does it make sense to use a dictionary of lists or should I approach it differently? Feedback would be really aprreciated – user3692506 Jun 02 '14 at 19:40

0 Answers0