Harshitha Goli Harshitha Goli - 2 months ago 15
Python Question

Use dictwriter and write unstructured data into csv file using python

Output CSv fileI am currently trying to put the data from a file into csv file using python.
My data looks as follows:

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'}]


My output should look like this :

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


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.

python 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)

Answer

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

Your list of one dictionary:

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'}]

First, you need to define your output columns:

output_cols = ['row', 't0', 'elems', 'Tspan', 'NP', 'wave0']

Then you need to create lists from the values of the dictionary and subsequently *zip* all those lists together:

# 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)

If you are using python3, then from itertools import izip_longest should be replaced with from itertools import zip_longest.

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

Content of somefile.csv:

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

I hope this helps.