2012-11-29 30 views
1

我剛開始學習Python,我需要幫助,我的實習要求我編寫腳本。使用python將特定數據從一個excel文件傳輸到另一個文件

我有一個csv文件(sheet1.csv),我需要從兩個具有標頭referenceID和PartNumber彼此對應的列中提取數據。我需要更新一個名爲sheet2.csv的單獨的csv文件,其中也包含兩列referenceID和PartNumber,但許多PartNumber單元格都是空的。

基本上我需要用sheet1中的值填寫「PartNumber」字段。從我所做的研究中,我決定使用字典是寫腳本的堅實途徑(我認爲)。到目前爲止,我已經能夠讀取文件並創建兩個字典,其中referenceIDs作爲關鍵字,PartNumber作爲值...這就是我所展示的字典樣例的示例。

import csv 
a = open('sheet1.csv', 'rU') 
b = open('sheet2.csv', 'rU') 
csvReadera = csv.DictReader(a) 
csvReaderb = csv.DictReader(b) 
a_dict = {} 
b_dict = {} 

for line in csvReadera: 
    a_dict[line["ReferenceID"]] = line["PartNumber"] 
print(a_dict) 

for line in csvReaderb: 
    b_dict[line["ReferenceID"]] = line["PartNumber"] 
print(b_dict) 

a_dict = {'R150': 'PN000123', 'R331': 'PN000873', 'C774': 'PN000064', 'L7896': 'PN000447', 'R0640': 'PN000878', 'R454': 'PN000333'} 
b_dict = {'C774': '', 'R331': '', 'R454': '', 'L7896': 'PN000000', 'R0640': '', 'R150': 'PN000333'} 

如何比較兩個詞典並填寫/覆蓋b-dict的缺失值然後寫入sheet2?當然,必須有比我提出的方法更有效的方法,但我以前從未使用過Python,所以請原諒我的可憐嘗試!

+0

我只是試圖讓第一款對眼睛:) – YXD

回答

0

看看熊貓圖書館。

import padas as pd 

#this is how you read 
dfa = pd.read_csv("sheet1.csv") 
dfb = pd.read_csv("sheet2.csv") 

令S絕對把你定義爲TESTDATA

a_dict = {'R150': 'PN000123', 'R331': 'PN000873', 'C774': 'PN000064', 'L7896': 'PN000447', 'R0640': 'PN000878', 'R454': 'PN000333'} 
b_dict = {'C774': '', 'R331': '', 'R454': '', 'L7896': 'PN000000', 'R0640': '', 'R150': 'PN000333'} 
dfar = pd.DataFrame(a_dict.items(), columns = ['ReferenceID', 'PartNumber']) 
dfbr = pd.DataFrame(b_dict.items(), columns = ['ReferenceID', 'PartNumber']) 
dfa = dfar[['ReferenceID', 'PartNumber']] 
dfa.columns = ['ReferenceIDA', 'PartNumberA'] 
dfb = dfbr[['ReferenceID', 'PartNumber']] 
dfb.columns = ['ReferenceIDB', 'PartNumberB'] 

的http://stardict.sourceforge.net/Dictionaries.php下載你得到這個

In [97]: dfa 
Out[97]: 
    ReferenceIDA PartNumberA 
0   R331 PN000873 
1   R454 PN000333 
2  L7896 PN000447 
3   R150 PN00
4   C774 PN000064 
5  R0640 PN000878 

In [98]: dfb 
Out[98]: 
    ReferenceIDB PartNumberB 
0   R331    
1   R454    
2  R0640    
3   R150 PN000333 
4   C774    
5  L7896 PN000000 

現在

In [67]: cd = pd.concat([dfa,dfb], axis=1) 

    In [68]: cd 
    Out[68]: 
    ReferenceIDA PartNumberA ReferenceIDB PartNumberB 
0   R331 PN000873   R331    
1   R454 PN000333   R454    
2  L7896 PN000447  R0640    
3   R150 PN00R150 PN000333 
4   C774 PN000064   C774    
5  R0640 PN000878  L7896 PN000000 




cd["res"] = cd.apply(lambda x : x["PartNumberB"] if x["PartNumberB"] else x["PartNumberA"], axis=1) 

cd 
Out[106]: 
    ReferenceIDA PartNumberA ReferenceIDB PartNumberB  res 
0   R331 PN000873   R331    PN000873 
1   R454 PN000333   R454    PN000333 
2  L7896 PN000447  R0640    PN000447 
3   R150 PN00R150 PN000333 PN000333 
4   C774 PN000064   C774    PN000064 
5  R0640 PN000878  L7896 PN000000 PN000000 

這是你想要什麼

剛剛成立

dfbr['PartNumber'] = cd['res'] 

和轉儲到csv

dfbr.to_csv('sheet2.csv') 
+0

非常感謝你的幫助更輕鬆一點!對於我的類型,熊貓庫看起來像是一個極其強大的工具,但我寧願暫時避免使用外部庫。再次,謝謝你的時間。 – scott0880

相關問題