I am accessing the data of a facebook page through its API, I have already managed to obtain an output but it is in JSON format and the intention is to convert it into a CSV but I have not succeeded.
import requests
import facebook
import urllib3
import csv
import json
import pandas as pd
token= 'EAAHAW2Bvoa8BAKgbnLe6r5ZAASjgIPbRl683UmcjQuBTwlSCJNJsVWPxmNSk013jcYVivSemjTivuRaEIxD918CuhSsIM7ZCCmDqKiHNi80ndSTy3oXZCklk3f4U9jIQGf20HOZBBqJtnyiWXGdZAQ6tq3mpaRNEmDMe47t4ERTLfPd1ja1yqWA67ZC6ZB0xylGjnStckbNudMaKT3w2Ub6'
graph = facebook.GraphAPI(access_token=token)
events = graph.request('/2063741620561889/insights?metric=page_fans,page_actions_post_reactions_like_total,page_actions_post_reactions_love_total,page_actions_post_reactions_wow_total,page_actions_post_reactions_haha_total,page_actions_post_reactions_sorry_total,page_actions_post_reactions_anger_total,page_video_views,page_video_views_organic,page_posts_impressions,page_impressions,post_clicks,page_views_total&period=day&date_preset=last_year')
Result
I would like to be able to get any result of that type and be able to convert it to a CSV. I have tried the following but without success:
info = json.dumps(events)
print(info[0].keys())
with open("samplecsv.csv", 'w') as f:
wr = csv.DictWriter(f, fieldnames = info[0].keys())
wr.writeheader()
wr.writerows(info)
CodePudding user response:
When you have a data not well structured like in your case, it can be done by analyzing the json and iterating through it to get what you need. In four specific case, it could be something like this:
data = [(x["value"],x["end_time"]) for x in info["data"][0]["values"]]
Whit this you can retrieve the info necessary in a list of tuples to save in a csv file like you where doing, but is not necessary to make yourself that file, you could use a library that do it for you, for example building a pandas data frame:
import pandas as pd
df = pd.DataFrame(data=data, columns=[info["data"][0]["name"], info["data"][0]["period"]])
df.to_csv(your_path)
There are other libraries that create this structures in an automatic way, but not always work as you intend. This is the easiest way that i could think to do it and that can be modified to other data.