2013-11-05 174 views
2

我有3個CSV文件。我有一個主要的CSV文檔,其中包含我需要的大部分信息。但缺少其他兩個CSV文檔的數據。目標是將主文檔與其他每個CSV進行比較,從2個CSV中提取指定的數據,並將該信息添加到主CSV中的行。最後將它們寫入一個新文件。將兩個CSV文件與主CSV文件進行比較,並將結果合併到一個新文件中

這是我到目前爲止,而不是它的作品。

INV =是主文件,它具有的信息99%I需要

VB =已在共同 '部件代碼' 與INV的 '\ XEF \ XBB \ xbfPART_CODE',我需要它的「手頭」添加到INV的排

主要 =有共同的「部件代碼」與INV的「\ XEF \ XBB \ xbfPART_CODE」,我需要它的「手頭」添加到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 

1問題:

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

當它添加到列表中,它把所有的結果爲「0」,如果我忽略的東西:dictline2 [「VB」] =「0」的一部分,它報告如預期。當我嘗試打印我[「VB」]

第二個問題「VB」:

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

如前所述,抱怨一個KeyError異常,但如果不留在,稍後抱怨KeyError異常的。如果我只是打印我,它顯示我的鍵/值是在那裏,它會報告'VB':'0'等。

我仍然是非常綠色的編程,在這個小時一個星期的晚上,我已經變得灰心了。但還不夠放棄!

+0

我不知道我完全理解你的問題。你有你的inv文件,它有一個'ON-HAND'列。我在解釋你的問題時是否正確,意思是你想用來自主或VB的結果來填充'PART CODE'列,這取決於哪些共享ON-HAND值與inv? –

+0

INV does not * have *手頭上,dictline2 ['VB'] = dictline1 ['ON-HAND']正在向INV中的行添加字典鍵值。 – user1764417

+0

如果您打算使用表格數據,則應該查看[pandas](http://pandas.pydata.org)庫。它使合併和對齊數據變得更容易。 – DSM

回答

0

首先,我想你打算讓他們產生對每個內環literation縮進追加聲明:

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 
+0

縮進最初是在正確的地方,按照你的建議嘗試,它會抱怨在原來的其他地方無意中縮進。你分享[:10]打印第一行是非常有幫助的。現在我可以看到我的失敗發生在接近瞬間而不是每2分鐘。 – user1764417

+0

將這兩個dictlines轉換爲列表也是一個不錯的提示。現在我可以可靠地打電話給我['VB']。仍然有問題,如果然後循環分配一切爲0。 – user1764417

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)