0

Hi programmers of the internet,

I'm fairly new into programming, and I'm trying to export data into a csv but there's a field on the json that the api returns which is a list containing several dictionaries.

This is what the field look like:

[{'field': 'custom_fields_41691425', 'value': 'tag_44'},
 {'field': 'comment_value_html',
  'value': '<p>Example comment</p>'}]

And herer is what I get when I open it with excel.

here

I'd like to get this estructure in excel:

HEADER     custom_fields_41691425  | comment_value_html
2nd row:   tag_44                  | <p>Example comment</p>

Is there any way to achieve this? Any help would be greatly appreciated.

Best regards.

AMC
  • 2,642
  • 7
  • 13
  • 35
  • you can use `for`-loop to convert it to something more useful and later write it to csv using standard module `csv`. This module should have also functions to works with dictionaries - you should find official documentation and read it. – furas Apr 23 '20 at 22:50
  • 1
    Does this answer your question? [How do I convert this list of dictionaries to a csv file?](https://stackoverflow.com/questions/3086973/how-do-i-convert-this-list-of-dictionaries-to-a-csv-file) – drum Apr 23 '20 at 22:59
  • Can you be more specific about what the issue is? Please see [ask], [help/on-topic]. Also, please do not share information as images unless absolutely necessary. See: https://meta.stackoverflow.com/questions/303812/discourage-screenshots-of-code-and-or-errors, https://idownvotedbecau.se/imageofcode, https://idownvotedbecau.se/imageofanexception/. – AMC Apr 23 '20 at 23:26

2 Answers2

0

Module csv can save dictionary but it would have to be

[
  {'custom_fields_41691425': 'tag_44', 'comment_value_html': '<p>Example comment</p>'},
  {'custom_fields_41691425': 'tag_44', 'comment_value_html': '<p>Example comment</p>'}
]

So you have to convert it

data = [

    [{'field': 'custom_fields_41691425', 'value': 'tag_44'},
     {'field': 'comment_value_html', 'value': '<p>Example comment</p>'}],

    [{'field': 'custom_fields_41691425', 'value': 'tag_44'},
     {'field': 'comment_value_html', 'value': '<p>Example comment</p>'}],

]

new_data = []

for row in data:
    new_row = {}
    for item in row:
        new_row[item['field']] = item['value']
    new_data.append(new_row)

print(new_data)

After that you can easily save it

import csv

header = new_data[0].keys()
#print(header)

with open('output.csv', 'w') as fh:
    csv_writer = csv.DictWriter(fh, header)

    csv_writer.writeheader()
    csv_writer.writerows(new_data)

Minimal working example

import csv

data = [

    [{'field': 'custom_fields_41691425', 'value': 'tag_44'},
     {'field': 'comment_value_html', 'value': '<p>Example comment</p>'}],

    [{'field': 'custom_fields_41691425', 'value': 'tag_44'},
     {'field': 'comment_value_html', 'value': '<p>Example comment</p>'}],

]

new_data = []

for row in data:
    new_row = {}
    for item in row:
        new_row[item['field']] = item['value']
    new_data.append(new_row)

print(new_data)

header = new_data[0].keys()
print(header)

with open('output.csv', 'w') as fh:
    csv_writer = csv.DictWriter(fh, header)

    csv_writer.writeheader()
    csv_writer.writerows(new_data)
furas
  • 134,197
  • 12
  • 106
  • 148
0
import pandas as pd
data = [{'field': 'custom_fields_41691425', 'value': 'tag_44'},

        {'field': 'comment_value_html', 'value': '<p>Example comment</p>'}]
x = []
for d in data:
    x.append(list(d.values()))

df = pd.DataFrame(x)
df.columns = ['field','value']
df.to_csv('data.csv',index=False)