Use dictwriter and write unstructured data into csv file using python
<p><a href="http://i.stack.imgur.com/Hb2z5.jpg" rel="nofollow">Output CSv file</a>I am currently trying to put the data from a file into csv file using python.
My data looks as follows:</p>

<pre><code>data_list{} = [{'row': '0', 't0': '8.69E-005', 'elems': ' 4 96 187 ', 'Tspan': '5E-006', 'NP': '625','wave0': '123.65 333.56 3333.78 567.89 345678.77 34E-08'}]
</code></pre>

<p>My output should look like this :</p>

<pre><code> row  t0        elems Tspan  NP  wave0
 0    8.69E-005  4    5E-006 625  123.65
                 96               333.56
                 187              3333.78
                                  567.89
                                  345678.77
                                  34E-08
</code></pre>

<p>First comes header and then values underneath them. I was succesfull in getting the header part and the row parts for all except for elems and wave0.</p>

<p>python code:  </p>

<pre><code> with open('cc.csv','w', newline='') as out_file:
     writer = csv.DictWriter(out_file, fieldnames=data_list[0].keys())
     writer.writeheader()
     for data in data_list:
          writer.writerow(data)
</code></pre>

<p>It looks like you are after the dictionary inside your <code>data_list</code> variable. In order to get your desired output, you are going to need to do a little bit of work:</p>

<p>Your list of one dictionary:</p>

<pre><code>data_list = [{'row': '0', 't0': '8.69E-005', 'elems': ' 4 96 187 ', 'Tspan': '5E-006', 'NP': '625','wave0': '123.65 333.56 3333.78 567.89 345678.77 34E-08'}]
</code></pre>

<p>First, you need to define your output columns:</p>

<pre><code>output_cols = ['row', 't0', 'elems', 'Tspan', 'NP', 'wave0']
</code></pre>

<p>Then you need to create lists from the <code>values</code> of the dictionary and subsequently <code>*zip*</code> all those lists together:</p>

<pre><code># Python2
from itertools import izip_longest
import csv

data = [data_list[0].get(x).strip().split(" ") for x in output_cols]
data = izip_longest(*data)
with open('somefile.csv','w') as outfile:
    file_writer = csv.writer(outfile, delimiter="\t", lineterminator="\n")
    file_writer.writerow(output_cols)
    file_writer.writerows(data)
</code></pre>

<p>If you are using python3, then <code>from itertools import izip_longest</code> should be replaced with <code>from itertools import zip_longest</code>.</p>

<p>Please note the answer assumes that all your data is that first dictionary (<code>data_list[0]</code>). If your actual list is longer than that, please share more data.</p>

<p>Content of <code>somefile.csv</code>:</p>

<pre><code>row     t0      elems   Tspan   NP      wave0
0     8.69E-005   4     5E-006  625     123.65
                 96                     333.56
                 187                    3333.78
                                        567.89
                                        345678.77
                                        34E-08
</code></pre>

<p>I hope this helps.</p>

python csv