Jan-24-2019, 09:08 AM
Firstly, apologies if I'm not using the right terminology, I'm trying to run before I walk and you will see the problems I'm having. I know how to code in other languages but I've got a tight deadline to meet on this one ....
As the title says I'm trying to get data from an API, do some slight massaging in pandas and write to a relational database.
When I was testing last week I was able to save the API output as a JSON file and read this into python and process it in pandas.
Now I want to read directly from the API and process it the same way. The API is confidential, so I can't post details about it here (but that's not the problem).
This is what I have so far:
Within a function I get the data from the API like this:
req = requests.get(...)
Return the data from the function like this:
return req.content
It seems to get returned as a bytestream, so I make it a string by doing this:
data = data.decode()
This still doesn't look like JSON format (but it's close), so I run a series of replace functions to get the data looking like the file and make it look like this:
![[Image: 2019-01-24-09-15-42-Command-Prompt-python.png]](https://i.ibb.co/2WmRF1S/2019-01-24-09-15-42-Command-Prompt-python.png)
But when I try to convert it to a dataframe:
df0 = pd.DataFrame(data)
I get an error.
Can anyone see what I'm doing wrong ?
Is there a better way to do this ?
As the title says I'm trying to get data from an API, do some slight massaging in pandas and write to a relational database.
When I was testing last week I was able to save the API output as a JSON file and read this into python and process it in pandas.
Now I want to read directly from the API and process it the same way. The API is confidential, so I can't post details about it here (but that's not the problem).
This is what I have so far:
Within a function I get the data from the API like this:
req = requests.get(...)
Return the data from the function like this:
return req.content
It seems to get returned as a bytestream, so I make it a string by doing this:
data = data.decode()
This still doesn't look like JSON format (but it's close), so I run a series of replace functions to get the data looking like the file and make it look like this:
![[Image: 2019-01-24-09-15-42-Command-Prompt-python.png]](https://i.ibb.co/2WmRF1S/2019-01-24-09-15-42-Command-Prompt-python.png)
But when I try to convert it to a dataframe:
df0 = pd.DataFrame(data)
I get an error.
Can anyone see what I'm doing wrong ?
Is there a better way to do this ?