0

I'm retrieving data from an API, but can not get it to save to a file.

import requests
import json
response = requests.get(f'url', headers={'CERT': 'cert'})
response = response.json()

When I run response or respons.json() I can see the data that I am wanting to record.

I have tried:

str1 = ''.join(map(str, response))

with open('data.txt', mode ='a+') as f:
    f.write(f'{str1}') 
  
    print("File appended successfully") 
f.close()
str1 = ''.join(map(str, response))

with open('data.json', mode ='a+') as f:
    f.write(f'{str1}') 
  
    print("File appended successfully") 
f.close()
with open('data.json', mode ='a+') as results:
    result = json.loads(results)
    results.write(json.dumps(result, indent=4))
with requests.get(f'url', headers={'CERT': 'cert'}, stream=True) as r:
    r.raise_for_status()
    with open('data.json', 'wb') as f_out:
        for chunk in r.iter_content(chunk_size=8192): 
            f_out.write(chunk)
with open('data.txt', mode ='a+') as f:
    for items in response: 
        f.write('%s\n' % items) 
  
    print("File appended successfully") 
f.close()
with open('data.json', mode ='a+') as f:
    for items in response: 
        f.write('%s\n' % items) 
  
    print("File appended successfully") 
f.close()

And a few other variations with no luck. Can someone point me in the right direction or let me know what I'm doing incorrectly to get the data from the response variable to actually populate in the file?

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Sai sei
  • 141
  • 1
  • 10
  • 1
    Does this answer your question? [Saving response from Requests to file](https://stackoverflow.com/questions/31126596/saving-response-from-requests-to-file) – mkrieger1 Mar 11 '21 at 11:13
  • Thanks for the quick response. But no none of those helped, the code started throwing errors with the list doesn't have attribute 'text' and list doesn't have attribute 'content' – Sai sei Mar 11 '21 at 12:15
  • please read [What should I do when someone answers my question?](https://stackoverflow.com/help/someone-answers) – Vishal Singh Mar 23 '21 at 05:01

2 Answers2

1

You can use json.dump(obj, fp) to serialize the object as a JSON formatted stream to a file object fp which supports .write() operation.

import json
with open('data.json', 'w') as fp:
    json.dump(your_json_response, fp)
Vishal Singh
  • 6,014
  • 2
  • 17
  • 33
  • Thanks for the quick reply, but no luck with this either. It doesn't throw an error so it looks like it completes, but it doesn't write to the file either. – Sai sei Mar 11 '21 at 12:18
  • try printing `response.json()` and if possible include the output in your post. – Vishal Singh Mar 11 '21 at 16:26
0

First let me apologize, it kept bugging me that none of the code I had found was working, especially with no error. I ran print(sys.path) and found that my output wasn't going where I wanted it, it was going into a completely different folder ... so it was working the whole time just outputting to a folder that I have no idea why it was outputting to. Thanks everyone for their help.

Sai sei
  • 141
  • 1
  • 10