Python Forum

Full Version: Next steps for using API data
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm very new to Python, and as a starter project I'm trying to use it to request data using an API which I then want to store in a csv file.

So far I have a working script that makes the API calls and stores the relevant data in a list. I'm a bit stuck as to where to go next to get this data into csv.

Each item in my list is a dictionary with some nested lists/dictionaries within. So I think I need to flatten the data before I can save to csv?

From the googling I've done so far, I think I may need to start looking at pandas - is this the right road to go down?

If anyone can suggest any useful resources/next steps I'd really appreciate it.
Why do you want to write the data to a CSV? If it has structure other than rows and columns there are better ways to represent that. Is some other application going to use the data? If so, the format is most likely set by the consuming application.
(Oct-09-2020, 01:20 PM)deanhystad Wrote: [ -> ]Why do you want to write the data to a CSV? If it has structure other than rows and columns there are better ways to represent that. Is some other application going to use the data? If so, the format is most likely set by the consuming application.

My ultimate goal is to import the data into a sql database, so I'm using csv as an intermediary step.
(Oct-09-2020, 03:36 PM)Rebster Wrote: [ -> ]My ultimate goal is to import the data into a sql database, so I'm using csv as an intermediary step.
why don't you write directly in the database?
i had the same thought.  python can go directly to a many databases, skipping the CSV step, completely.
A part from a csv not being necessary, your question may be triggered by the apparent
spagetti jason response. A similar question came up a few months ago.
One possibility is to loop through the API response, testing each item for its type:
if type(v) == str: (or dict, or list...)
That provides you with flexibility to put the elements you're interested in directly into a db.
Paul
"I'm trying to use it to request data using an API"

While doing so you may want to keep eye on legal status of API copyright which is currently reviewed by US Supreme court because Supreme Court justices seem poised to allow copyrights on APIs.

Pending on court decision, your jurisdiction and particular API you may found yourself in muddy legal waters while "using an API".