'Exporting API output response to CSV - Python

I am not an expert in Python but I used it to call data from an API. I got a code 200 and printed part of the data but I am not able to export/ save/ write the output (to CSV). Can anyone assist?

This is my code:

import requests
headers = {
    'Accept': 'text/csv',
    'Authorization': 'Bearer ...'
}

response = requests.get('https://feeds.preqin.com/api/investor/pe', headers=headers)
print response
output = response.content

And here is how the data (should be CSV, correct?) looks like: enter image description here

I managed to save it as txt but the output is not usable/ importable (e.g. to Excel). I used the following code:

text_file = open("output.txt", "w")
n = text_file.write(output)
text_file.close()

Thank you and best regards,

A



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source