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', 'admin@planet.nl'] ,
['Cautus  B.V.', 'Cautus  B.V.Wei 9-11', 'Wei 9-11', '1019123', '10', 'AVB', 'Geachte mevrouw Daa', 'Mevrouw C.P. Daa', 'admin@planet.nl'] ,
['Cautus  B.V.', 'Cautus  B.V.plein 92', 'plein 92', '1129008', '10', 'BEDR', 'Geachte mevrouw Daa', 'Mevrouw C.P. Daa', 'admin@planet.nl'] ,
['Cautus  B.V.', 'Cautus  B.V.Wei 9-11', 'Wei 9-11', '1019123', '10', 'BEDR', 'Geachte mevrouw Daa', 'Mevrouw C.P. Daa', 'admin.@planet.nl'] ,
['De company', 'De companytiellaan 42', 'tiellaan 42', 'KD0022232', '13', 'AVB', 'Geachte heer Tigch', 'De heer I. Tigch', 'imre@company.nl'] ,
['De company', 'De companytiellaan 42', 'tiellaan 42', 'KD0022232', '13', 'DAS', 'Geachte heer Tigch', 'De heer I. Tigch', 'imre@company.nl'] ,
['Slever ', 'Slever klopt 42', 'klopt 42', 'KD2220115', '17', 'AVB', 'Geachte heer Slever', 'De heer T. Slever', 'info@company.com']]

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

如您所见,他只在其中写了一个街道名称。

谁能帮我解决这个问题?

4

1 回答 1

5

您只有一个列表(您在每个循环中重置klantgegevens),并且您编写该列表,就好像它是一组完整的多行。

CSV 模块将该列表视为一组序列,这意味着每个字符串条目都被视为单个字符的序列,然后将其写入您的 csv 文件:

>>> klantgegevens
['Slever klopt 42', 'Slever ', ['AVB'], 'klopt 42', 'KD2220115', '17', 'Geachte heer Slever', 'De heer T. Slever', 'info@company.com']
>>> 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)
于 2012-10-19T12:36:44.627 回答