将两个CSV文件与主CSV文件进行比较,并将结果合并到一个新文件中

时间:2013-11-05 03:56:13

标签: python python-2.7 csv dictionary

我有3个CSV文件。我有一个主CSV文档,其中包含我将需要的大量信息。但是缺少其他两个CSV文档的数据。目标是将主文档与每个其他CSV进行比较,从2个CSV中提取指定数据,并将该信息添加到主CSV中的行。最后将它们写入新文件。

这是我到目前为止所得到的,而不是它有效。

inv =是主文档,它有99%我需要的信息

vb =与inv的'\ xef \ xbb \ xbfPART_CODE'有共同的'PART CODE',我需要将'ON-HAND'添加到inv的行

main =与inv的'\ xef \ xbb \ xbfPART_CODE'有共同的'PART CODE',我需要将'ON-HAND'添加到inv的行

import csv

inv = csv.DictReader(open('ireport.txt', 'rU'), dialect='excel-tab', delimiter="\t")
vb = csv.DictReader(open('vb.txt', 'rU'), dialect='excel-tab', delimiter="\t")
main = csv.DictReader(open('main.txt', 'rU'), dialect='excel-tab', delimiter="\t")

vblist = []
mainlist = []

def vbfunc(dictreader1, dictreader2):
        dictreader2 = list(dictreader2)
        for dictline1 in dictreader1:
            for dictline2 in dictreader2:
                if dictline1['PART CODE'] == dictline2['\xef\xbb\xbfPART_CODE']:
                    dictline2['VB'] = dictline1['ON-HAND']
                else:
                    dictline2['VB'] = "0" 
        vblist.append(dictline2)

def mainfunc(dictreader1, dictreader2):
        dictreader2 = list(dictreader2)
        for dictline1 in dictreader1:
            for dictline2 in dictreader2:
                if dictline1['PART CODE'] == dictline2['\xef\xbb\xbfPART_CODE']:
                    dictline2['MAIN'] = dictline1['ON-HAND']
                else:
                    dictline2['MAIN'] = "0"
        mainlist.append(dictline2)

vbfunc(vb,inv)
#mainfuc(main,inv)   #I'll get to this when the other function works

for i in vblist:
    print i['VB']  #complains of KeyError: VB

第一个问题:

if dictline1['PART CODE'] == dictline2['\xef\xbb\xbfPART_CODE']:
    dictline2['VB'] = dictline1['ON-HAND']
else:
    dictline2['VB'] = "0"

当它被添加到列表中时,它将每个结果视为“0”,如果我省略了else:dictline2 ['VB'] =“0”部分,它会按预期报告。但如果没有留下来,它会在以后抱怨KeyError:'VB'当我尝试打印i ['VB']

第二个问题:

for i in vblist:
    print i['VB']  #complains of KeyError: VB

如前所述,抱怨KeyError。如果我只是打印i,它会显示i的键/值在那里,它会报告'VB':'0'等。

我对编程仍然非常环保,在这个时间过了一个星期的一个星期,我变得气馁。但还不足以放弃!

2 个答案:

答案 0 :(得分:0)

首先,我认为你打算缩进append语句,以便它们出现在每个内部循环文字中:

    for dictline1 in dictreader1:
        for dictline2 in dictreader2:
            if dictline1['PART CODE'] == dictline2['\xef\xbb\xbfPART_CODE']:
                dictline2['VB'] = dictline1['ON-HAND']
            else:
                dictline2['VB'] = "0" 
            vblist.append(dictline2)    #indented

除此之外,从您提供的描述中解析问题有点困难。我建议在每个阶段打印几行以确认数据确实按照您的预期处理。例如,如果您还将dictline1转换为列表,则可以获取每个文件的一小部分:

def vbfunc(dictreader1, dictreader2):
    #iterate over 5 rows each
    dictreader1 = list(dictreader1)[:5] 
    dictreader2 = list(dictreader2)[:5]

    for dictline1 in dictreader1:
        for dictline2 in dictreader2:

            #print what's being read... 
            print '\nLines 1, 2', dictline1, dictline2
            print '\nColumns 1, 2', dictline1['PART CODE'], dictline2['\xef\xbb\xbfPART_CODE']

            if dictline1['PART CODE'] == dictline2['\xef\xbb\xbfPART_CODE']:
                dictline2['VB'] = dictline1['ON-HAND']
            else:
                dictline2['VB'] = "0" 

            #print the modified line
            print '\n New line 2', dictline2
            vblist.append(dictline2)    #indented

print '\n Modified lines ', vbList[:10] #print first 10 rows of output together

如果你过去上面正在处理大量数据,你也可以特别注意KeyErrors:

for row in vbList:
    try:
        print row['VB']
    except KeyError:
        print row

答案 1 :(得分:0)

我让这个工作!这有点笨拙,但就我的目的而言,我认为我可以做到这一切。

import csv

inv = csv.DictReader(open('ireport.txt', 'rU'), dialect='excel-tab', delimiter="\t")
vb = csv.DictReader(open('vb.txt', 'rU'), dialect='excel-tab', delimiter="\t")
main = csv.DictReader(open('main.txt', 'rU'), dialect='excel-tab', delimiter="\t")


def vbfunc(dictreader1, dictreader2):
        dictreader2 = list(dictreader2)
        dictreader1 = list(dictreader1)
        vblist = []
        for dictline1 in dictreader1:
            for dictline2 in dictreader2:
                if dictline1['PART CODE'] == dictline2['\xef\xbb\xbfPART_CODE']:
                    dictline2['VB'] = dictline1['ON-HAND']
                else:
                    continue
        vblist.append(dictline2)
    for i in vblist:
        print i['BARCODE'],i['VB']

def mainfunc(dictreader1, dictreader2):
        dictreader2 = list(dictreader2)[:5000]
        dictreader1 = list(dictreader1)[:5000]
        mainlist = []
        for dictline1 in dictreader1:
            for dictline2 in dictreader2:
                if dictline1['PART CODE'] == dictline2['\xef\xbb\xbfPART_CODE']:
                    dictline2['MAIN'] = dictline1['ON-HAND']
                else:
                    continue
        mainlist.append(dictline2)
    for i in mainlist:
        print i['BARCODE'],i['MAIN']


vbfunc(vb,inv)
mainfunc(main,inv)