Reputation: 179
I have a large list, contains a series of ids and related values, extremely shortened version looks like so:
large = [('550111', [(4, 5, 8), (6, -4, -6)]), ('222211', [(2, -4, 5), (1, 15, -4)])]
I want to export this to excel in a way that 5501
will be in A1
, then the first set of values will be under it in B1
, B2
& B3
and the next set in C1
, C2
, C3
, then have a space then 2222
in E1
, with the first set of values relating to it in F1
, F2
& F3
, the second in G1
, G2
, G3
. i.e. in Excel (every value with its own Excel rectangle thing to itself) like:
550111
4 5 8
6 -4 -6
'space here (dont know how to add one)'
222211
2 -4 5
1 15 -4
'SPACE HERE AGAIN'
I have tried the follwoing:
writer_jobby = csv.writer(open('poo5.csv', 'wb'), delimiter=',')
for i in large:
if i == '[':
i.replace()
if i == ']':
i.replace()
if i == '(':
i.replace()
if i == ')':
i.replace()
else:
writer_jobby.writerow(i)
But I get absolute nonsense, as I am struggling with how to deal with the []
and the ()
in my list. I would really appreciate if someone could shed some light on how I could deal with the data so I could export it in the desired way. Thank you.
Help much appreciated.
EDIT
new desired form incl. absolute max:
550111
4,5,8
6,-4,-6
'space here'
'Max:'
6, 5, 8
'space here'
222211
2,-4,5
1,15,-4
'space here'
'Max:'
2, 15, 5
'space here'
1, 2, 3, ........, 8904
Upvotes: 2
Views: 138
Reputation: 353059
Maybe something like this would help?
import csv
large = [('5501', [(4, 5, 8), (6, -4, -6)]), ('2222', [(2, -4, 5), (1, 15, -4)])]
with open("out1.csv", "wb") as fp: # open the file, call it fp, and autoclose it
writer = csv.writer(fp, delimiter=",")
for entry in large:
writer.writerow([entry[0]]) # one-element list
for line in entry[1]: # loop over each tuple in the second element
writer.writerow(line)
writer.writerow([]) # write an empty row
This produces
localhost-2:coding $ cat out1.csv
5501
4,5,8
6,-4,-6
2222
2,-4,5
1,15,-4
The csv
module is typically used by writing rows from elements. I don't know what final
is, but it looks like you're trying to do something with a string made from large
, which isn't going to work very well.
Upvotes: 1