2

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 
user1532369
  • 179
  • 1
  • 1
  • 10

1 Answers1

1

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.

DSM
  • 342,061
  • 65
  • 592
  • 494
  • oh and 'final' and 'large' were the same thing I forgot to change the name – user1532369 Aug 10 '12 at 14:24
  • any suggestion as to how I could add the word 'Max' after the space at the end of each set of values and underneath it add in the the maxs from each above, then another space...also after the very lasy space adding the numbers 1 to 8904? Please see my edit above – user1532369 Aug 10 '12 at 15:10
  • @user1532369: if you have a separate (and very different) question, then please open a new question and don't edit this one. SO questions work best when they're discrete and specific. – DSM Aug 10 '12 at 15:16
  • @user1532369: no worries. But I'd give your new problem a go yourself first-- one of the first questions you'll get is "what have you tried?" – DSM Aug 10 '12 at 15:18
  • http://stackoverflow.com/questions/11904832/modifying-a-non-ideal-list-format-to-excel-and-exporting-it I have but I do not know how to apply max and absolute functions to the indiviual values inside the list – user1532369 Aug 14 '12 at 09:31