使用python

时间:2015-06-12 00:06:35

标签: python-2.7 csv

我有一个csv文件,其中包含一条记录,我需要将变量分配给python脚本并运行。我的日期看起来像下面这一行。有标题。

"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

需要为每个值分配一个变量

第一名,“American Grocers”=公司名称

第二名,“Allison Smith”=联络

第3位=街道等

日期过后变得棘手。最后11个值彼此相关并保存到密钥。

如果值7 = 1,则变量7 =“apples”,变量8 = 10,否则跳过值7和8并转到9

如果值9 = 1,则变量9 =“oranges”,值10 =位置10(4)中的变量,否则跳过值9和10并转到11

如果值11 = 1,则变量11 =“桃子”,值12 =位置10(4)中的变量,否则跳过值11和12并转到13

如果值13 = 1,则变量13 =“pumkins”,值13 =位置13(2)中的变量,否则跳过值13和14

如果值15 = 1则变量15 =“交货”,否则变量=“取货”

因此python将分配以下内容:

CompanyName =“American Grocers”

联络=“Allison Smith”

Street =“456 1st.Street”

CityZip =“Podunk,California 00990”

Store =“Store 135 Order”

OrderDate(不一定是日期类型)=“05/14/2015”

orderList = {“apples”:10,“peaches”:4,“南瓜”:2}

接受=“接机”

我需要在脚本中进一步操作这些变量。

我有以下代码将数据输出到相应的标题信息。

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()

输出看起来像这样。 (我不确定为什么输出与文件的顺序不一样......)

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

我不知道如何接受这个并获得上面列出的变量。建议?使用python 2.7

2 个答案:

答案 0 :(得分:0)

  

我不确定为什么输出与文件的顺序不一样......

在Python字典中,条目以任意顺序显示。

以下是如何解析程序的一般轮廓。详细的逻辑:“如果这个字段是这样做,否则这样做”是我希望你可以自己做的事情。具体细节太长,详细,具体,对任何人都有价值,我猜这就是为什么对此并不感兴趣。

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()

答案 1 :(得分:-1)

我终于用以下代码完成了我想要的东西:感谢所有提供帮助的人。你的想法刺激了我所需的切线。

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"