2012-10-19 85 views
0

這是我的腳本:導出不完整

from itertools import groupby 
import operator 
import csv 

l = [['Cautus B.V.', 'Cautus B.V.plein 92', 'plein 92', '1129008', '10', 'AVB', 'Geachte mevrouw Daa', 'Mevrouw C.P. Daa', '[email protected]'] , 
['Cautus B.V.', 'Cautus B.V.Wei 9-11', 'Wei 9-11', '1019123', '10', 'AVB', 'Geachte mevrouw Daa', 'Mevrouw C.P. Daa', '[email protected]'] , 
['Cautus B.V.', 'Cautus B.V.plein 92', 'plein 92', '1129008', '10', 'BEDR', 'Geachte mevrouw Daa', 'Mevrouw C.P. Daa', '[email protected]'] , 
['Cautus B.V.', 'Cautus B.V.Wei 9-11', 'Wei 9-11', '1019123', '10', 'BEDR', 'Geachte mevrouw Daa', 'Mevrouw C.P. Daa', '[email protected]'] , 
['De company', 'De companytiellaan 42', 'tiellaan 42', 'KD0022232', '13', 'AVB', 'Geachte heer Tigch', 'De heer I. Tigch', '[email protected]'] , 
['De company', 'De companytiellaan 42', 'tiellaan 42', 'KD0022232', '13', 'DAS', 'Geachte heer Tigch', 'De heer I. Tigch', '[email protected]'] , 
['Slever ', 'Slever klopt 42', 'klopt 42', 'KD2220115', '17', 'AVB', 'Geachte heer Slever', 'De heer T. Slever', '[email protected]']] 

sortkey = operator.itemgetter(1,5) 
l_clean = sorted(l,key=sortkey) 
l_final = [(k, list(v)) for k,v in groupby(l_clean, key = operator.itemgetter(1))] 

for k,v in l_final: 
    info_rest = v[0][:5]+v[0][5:] 
    info_combine = map(operator.itemgetter(5),v) 
    uniekid = k 
    verz = info_combine 
    naam = info_rest[0] 
    risicoadr = info_rest[2] 
    polisnummer = info_rest[3] 
    relatienummer = info_rest[4] 
    aanhef = info_rest[6] 
    contactpersoon = info_rest[7] 
    emailadr = info_rest[8] 
    klantgegevens = [uniekid,naam,verz,risicoadr,polisnummer,relatienummer,aanhef,contactpersoon,emailadr,] 

import csv 
    with open('export.csv', 'w') as f: 
     writer = csv.writer(f) 
     writer.writerows(klantgegevens) 

當我寫我得到這個在我的.csv文件的結果:

S l e v e r  k l o p t  4 2 

正如你可以看到,他只寫一個它的街道名稱。

任何人都可以幫助我?

+2

你有什麼期望這做?您只需向您的CSV文件寫入一行。如果你想讓它寫多行,你應該將'writer.writerows(...)'移到你的循環中。 – Matt

回答

5

您只有一個列表(您在每個循環中重置klantgegevens),並且您將該列表編寫爲好像它是一整行多行。

csv模塊看到一個列表作爲一組序列,這意味着每個字符串項被視爲單個字符的序列,而這正是將被寫入到CSV文件:

>>> klantgegevens 
['Slever klopt 42', 'Slever ', ['AVB'], 'klopt 42', 'KD2220115', '17', 'Geachte heer Slever', 'De heer T. Slever', '[email protected]'] 
>>> list(klantgegevens[0]) 
['S', 'l', 'e', 'v', 'e', 'r', ' ', 'k', 'l', 'o', 'p', 't', ' ', '4', '2'] 

你可以分開來寫的每一行,同時收集klantgegevens列表:

import csv 
with open('export.csv', 'w') as f: 
    writer = csv.writer(f) 
    for k,v in l_final: 
     info_rest = v[0][:5]+v[0][5:] 
     info_combine = map(operator.itemgetter(5),v) 
     uniekid = k 
     verz = info_combine 
     naam = info_rest[0] 
     risicoadr = info_rest[2] 
     polisnummer = info_rest[3] 
     relatienummer = info_rest[4] 
     aanhef = info_rest[6] 
     contactpersoon = info_rest[7] 
     emailadr = info_rest[8] 
     klantgegevens = [uniekid,naam,verz,risicoadr,polisnummer,relatienummer,aanhef,contactpersoon,emailadr,] 
     writer.writerow(klantgegevens) 

現在,當你完成它的名單將被視爲列的順序,寫每一行。

或者,你必須收集每個klantgegevens列表到結果列表:

results = []  
for k,v in l_final: 
    # processing 
    klantgegevens = [uniekid,naam,verz,risicoadr,polisnummer,relatienummer,aanhef,contactpersoon,emailadr,] 
    results.append(klantgegevens) 

然後列出的該名單寫信給你的CSV文件:

import csv 
with open('export.csv', 'w') as f: 
    writer = csv.writer(f) 
    writer.writerows(results) 
+0

Thx第一個選項爲我工作 – sjeggiepop