This is my script:
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)
When i write i get this as a result in my .csv file:
S l e v e r k l o p t 4 2
As you can see he only writes one street name in it.
Anyone who can help me with this?
You only have one list (you reset
klantgegevenson each loop), and you write that list as if it is a full set of multiple rows.The CSV module sees that one list as a set of sequences, meaning that each string entry is seen as a sequence of individual characters, and that is what is then written to your csv file:
You can write each row separately while collecting the
klantgegevenslists:Now the list will be treated as a sequence of columns, writing each line as you complete it.
Alternatively, you’d have to collect each
klantgegevenslist into a results list:Then write that list of lists to your CSV file: