-1

i'have an issue to write my api call into one csv file . I'm new on Python, looked arround stackoverflow but can't figure it out . Anyone has an idea how to do it exactly ?

here is the answer ot the call https://i.stack.imgur.com/HqBm1.jpg

here is how i would like to get the https://i.stack.imgur.com/eKpeu.jpg

Many thanks,

and here is the code

import datetime

from facebookads.adobjects.adaccount import AdAccount
from facebookads.adobjects.adsinsights import AdsInsights
from facebookads.api import FacebookAdsApi

start_date = datetime.date.fromordinal(datetime.date.today().toordinal()-1).strftime("%F")
end_date = datetime.date.fromordinal(datetime.date.today().toordinal()-1).strftime("%F")


access_token = 'xxx'
ad_account_id = 'act_xx'
##app_secret = 'xxx'
FacebookAdsApi.init(access_token=access_token)

fields = [

'campaign_name',
'impressions',
'clicks',
'social_impressions',
'total_actions',

]
params = {
'time_range': {'since':start_date,'until':end_date},
##  'filtering': [],
'level': 'campaign',
##'breakdowns': ['days_1'],

}

print (AdAccount(ad_account_id).get_insights(
fields=fields,
params=params,
 ))

when trying the solution of N. Ivanov , i still get an error regarding :

f = csv.writer(open'testfbhh.csv', 'wb+'
sof
  • 3
  • 3
  • Possible duplicate of [How can I convert JSON to CSV?](https://stackoverflow.com/questions/1871524/how-can-i-convert-json-to-csv) – N. Ivanov Aug 14 '17 at 14:06
  • N. Ivanov, I tried this solution but i'm getting an error, invalid syntax for : f = csv.writer(open'test.csv', 'wb+') – sof Aug 14 '17 at 14:28
  • well you obviously have an error there... I would suggest you to do some python tutorials first. Otherwise you are missing `()` around the `open` call. – N. Ivanov Aug 14 '17 at 14:38
  • as i already said i'm learning, If you can redirect me it's nice otherwise i'l wish you a good day. i now have this error: the json object must be str, bytes or bytearray. I assume this is my results are not a string but the result of my request. – sof Aug 14 '17 at 15:05
  • [TutorialsPoint](https://www.tutorialspoint.com/python/) can get you started. Hope this helps! – N. Ivanov Aug 14 '17 at 15:07
  • thx for the link – sof Aug 14 '17 at 15:38

1 Answers1

0

with the help of Ivanov, i made a solution which is working fine.

I just wanted to know if there is a way to optimize it as i think it could be better.

thanks,

print (result,file = open ('export.json', 'w'))
with open('export.json', 'r') as file :
filedata = file.read()

# Replace the target which don't allow to convert my json into csv
filedata = filedata.replace('<AdsInsights> ', '')

# Write the file out again
with open('export.json', 'w') as file:
file.write(filedata)

with open('export.json') as json_data:
x = json.load(json_data)

f = csv.writer(open("test.csv", "w", newline=''))

# Write CSV Header, If you dont need that, remove this line


for x in x:
f.writerow([x["date_start"],
            x["date_stop"],
            x["impressions"],
            x["social_impressions"],
            x["total_actions"]])
sof
  • 3
  • 3