-1

I have a csv file that has a single record in it that I need to assign variables to and run through a python script. My date looks like the line below. Has header.

"CompanyName","Contact","Street","CityZip","Store","DateRec","apples","appQuan","oranges","orgQuan","peaches","peaQuan","pumpkins","pumQuan","Receive",0
American Grocers","Allison Smith","456 1st. Street","Podunk, California 00990","Store 135 Order","05/14/2015",1,10,0,4,1,4,2,0

Each value needs to be assigned a variable

1st position, "American Grocers" = CompanyName

2nd position, "Allison Smith" = Contact

3rd position = Street, etc.

After the date it gets tricky. The last 11 values are related to each other and get saved to a key.

If value 7 = 1, then variable 7 = "apples" and variable 8 = 10, else skip values 7 and 8 and go to 9

If value 9 = 1, then variable 9 = "oranges" and value 10 = the variable in position 10 (4), else skip values 9 and 10 and go to 11

If value 11 = 1, then variable 11 = "peaches" and value 12 = the variable in position 10 (4), else skip values 11 and 12 and go to 13

If value 13 = 1, then variable 13 = "pumkins" and value 13 = the variable in position 13 (2), else skip values 13 and 14

If value 15 = 1 then variable 15 = "Delivery", else variable = "Pick up"

thus python would assign the following:

CompanyName = "American Grocers"

Contact = "Allison Smith"

Street = "456 1st. Street"

CityZip = "Podunk, California 00990"

Store = "Store 135 Order"

OrderDate (does not need to be date type) = "05/14/2015"

orderList = {"apples" : 10, "peaches" : 4, "pumpkins" : 2}

Recieve = "Pick up"

I need to manipulate these variables further along in the script.

I have the following code to which outputs the data to its corresponding header information.

import csv

MY_FILE = "C:\\tests\\DataRequestsData\\qryFruit.csv"

def parse(raw_file, delimiter):
    opened_file = open(raw_file)
    csv_data = csv.reader(opened_file, delimiter=delimiter)
    parsed_data = []
    fields = csv_data.next()
    for row in csv_data:
        parsed_data.append(dict(zip(fields, row)))
    opened_file.close()
    return parsed_data

def main():

    new_data = parse(MY_FILE, ",")

    print new_data

if __name__ == "__main__":
    main()

Output looks like this. (I am not sure why the output is not in the same order as the file ...)

[{'DateRec': '05/14/2015', 'orgQuan': '4', 'CompanyName': 'American Grocers', 'appQuan': '10', 'peaQuan': '4', 'oranges': '0', 'peaches': '1', 'Contact': 'Allison Smith', 'CityZip': 'Podunk, California 00990', 'pumpkins': '2', 'apples': '1', 'pumQuan': '0', 'Store': 'Store 135 Order', 'Street': '456 1st. Street'}]

I do not know how to take this and get the variables assigned as listed above. Suggestions? Using python 2.7

user12059
  • 733
  • 2
  • 13
  • 27
  • This is the same question as [Creating a dictionary from a csv file?](http://stackoverflow.com/questions/6740918/creating-a-dictionary-from-a-csv-file) – rocky Jun 12 '15 at 00:26
  • Not really. I only need to create keys for the items ordered and quantity ordered (orderList) and then only those that were ordered (=1). The other values are "normal" variable replacements. When you see "Contact", put "Allison Smith". – user12059 Jun 12 '15 at 15:50

2 Answers2

0

I am not sure why the output is not in the same order as the file ...

In a Python dictionary, entries are displayed an arbitrary order.

Below is the general contour of how to parse the program. The detailed logic: "if this field is this do this otherwise do that" is something that I hope you can do on your own. The specifics are way too long, detailed, and specific to be of interest of value for anyone else and I'm guessing that is why there hasn't been much interest in this.

import csv

MY_FILE =  "C:\\tests\\DataRequestsData\\qryFruit.csv"

def parse(raw_file, delimiter):
    parsed_data = []
    with open(raw_file) as opened_file:
        rec = {}
        csv_data = csv.reader(opened_file, delimiter=delimiter)
        fields = csv_data.next()
        for row in csv_data:
            for i, val in enumerate(row[0:6]):
                rec[fields[i]] = val
            # This part below is too specific, long, and complicated
            # that it is doubtful filling this out in detail will be use
            # or interest to anyone else on stackoverflow. But to give
            # you an idea of how to proceed...
            if row[6] == '1':
                rec[fields[6]] = 'apples'
                rec[fields[7]] = 10
            else:
                # continue
                pass
            # ...
            parsed_data.append(rec)

    return parsed_data

def main():
    new_data = parse(MY_FILE, ",")
    print new_data

if __name__ == "__main__":
    main()
rocky
  • 7,226
  • 3
  • 33
  • 74
-1

I finally accomplished what I was looking for with the following code: Thank you to all who offered help. Your ideas spurred me onto the tangents I needed.

import csv
MY_FILE = csv.reader(open("C:\\tests\\DataRequestsData\\qryFruit.csv", "rb"))

for row in MY_FILE:
    CompanyName, Contact, Street, CityZip, Store, DateRec, apples, appQuan, oranges, orgQuan, peaches, peaQuan, pumpkins, pumQuan, Receive = row

    s='{'
if apples == "1":
    s = s + '"apples"' + ":" + appQuan
if oranges == "1":
    s = s + '", "oranges"' + ":" + orgQuan
if peaches == "1":
    s = s + '", "peaches"' + ":" + peaQuan
if pumpkins == "1":
    s = s + '", "pumpkins"' + ":" + pumQuan
s = s + '}'

if Receive == "0":
    Receive = "Pick up"
else:
    Receive = "Deliver"
user12059
  • 733
  • 2
  • 13
  • 27
  • What happened to the taking into account field names in header line? This is mentioned in your title. And now it looks like you are just processing the last line. It is not clear how one gets from your problem statement to this solution. – rocky Jun 15 '15 at 16:06