Reputation: 99
I have this list:
[Header, 132, mail@wurst, [adasdsd, 1, 3, 4], [sdsdsd, 12, 3, 4], [sdsds, 3, 3]]
and want to save it as a csv file and import it after transfer via ftp as the same list. I want:
[Header, 132, mail@wurst, [adasdsd, 1, 3, 4], [sdsdsd, 12, 3, 4], [sdsds, 3, 3]]
But i cant find a way to have the same list structure after importing the list. whatever solution from stackoverflow i use it always changes the structure of my list after import.
NO Solutions with:
Only Solutions using either:
Upvotes: 0
Views: 75
Reputation: 38422
If you know the header length (3 in this case) you can hardcode it in. Otherwise you will have to do something really tacky, or deal with the header like every other record.
from __future__ import print_function
import csv
listing = [
'Header', 132, 'mail@wurst',
['adasdsd', 1, 3, 4],
['sdsdsd', 12, 3, 4],
['sdsds', 3, 3]
]
with open('listing.csv', 'w') as outfile:
csvout = csv.writer(outfile)
csvout.writerow(listing[:3])
csvout.writerows(listing[3:])
with open('listing.csv') as infile:
csvin = csv.reader(infile)
header = next(csvin)
listing = [row for row in csvin]
print(header + listing)
[Added later...] Of course, if the CSV file already exists, no hardcoding need be done. Read the file first, and you already have the header separated out from the remainder.
Upvotes: 1