0

i have a file bundle.crd. The itens are like this

col1 val1 val2 val3 val4 
col2 val1 val2 val3 val4 
col4 val1 val2 val3 val4 
col5 val1 val2 val3 val4 
col6 val1 val2 val3 val4 
col7 val1 val2 val3 val4 

I need this to be imported to an excel file in the following format.

col1  col2  col3  col4  col5  col6 col7
-------------------------------------------
val1  val1  val1  val1  val1  val1  val1 
val2  val2  val2  val2  val2  val2  val2 
val3  val3  val3  val3  val3  val3  val3
val4  val4  val4  val4  val4  val4  val4 

I tried this code but its not working


    lines = (line for line in stripped if line)
        grouped = itertools.izip(*[lines] * 3)
        with open('extracted.csv', 'w') as out_file:
            writer = csv.writer(out_file)
            #writer = csv.writer(out_file, dialect=csv.excel)
            writer.writerow(('title', 'intro', 'tagline'))
            writer.writerows(grouped)

Please Help

jww
  • 97,681
  • 90
  • 411
  • 885
dvs
  • 511
  • 1
  • 10
  • 27
  • 1
    Can you be more clear about what is not working - are you getting an error? is it not giving the right output? something else? – Krease Oct 05 '14 at 05:34
  • It is storing it into the same format.ie; col1val1val2val3val4 into a single cell – dvs Oct 05 '14 at 05:41
  • 1
    [This](http://stackoverflow.com/questions/13437727/python-write-to-excel-spreadsheet?rq=1) might be useful for you. – ZAT Oct 05 '14 at 08:14
  • Thanks for the link. Again how will I create the list if I dont know the number of elemets – dvs Oct 05 '14 at 08:20

1 Answers1

0

Did you split the lines into cells?

Here is an example, how to transpose a list of lists:

>>> values
[['col1', 'val1', 'val2', 'val3', 'val4'], ['col2', 'val1', 'val2', 'val3', 'val4'], ['col4', 'val1', 'val2', 'val3', 'val4'], ['col5', 'val1', 'val2', 'val3', 'val4'], ['col6', 'val1', 'val2', 'val3', 'val4'], ['col7', 'val1', 'val2', 'val3', 'val4']]
>>> zip(*values)
[('col1', 'col2', 'col4', 'col5', 'col6', 'col7'), ('val1', 'val1', 'val1', 'val1', 'val1', 'val1'), ('val2', 'val2', 'val2', 'val2', 'val2', 'val2'), ('val3', 'val3', 'val3', 'val3', 'val3', 'val3'), ('val4', 'val4', 'val4', 'val4', 'val4', 'val4')]
Daniel
  • 42,087
  • 4
  • 55
  • 81