1

我有 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" 部分,它会按预期报告。但如果没有留下,它稍后会在我尝试打印 i['VB'] 时抱怨 KeyError: 'VB'

第二个问题:

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

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

我对编程仍然很陌生,一个星期每晚都在这个时间,我变得灰心了。但还不足以放弃!

4

2 回答 2

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)
于 2013-11-05T13:04:50.297 回答
0

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

    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
于 2013-11-05T06:14:25.797 回答