Python Forum
Next steps for using API data - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Next steps for using API data (/thread-30143.html)



Next steps for using API data - Rebster - Oct-09-2020

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.


RE: Next steps for using API data - deanhystad - Oct-09-2020

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.


RE: Next steps for using API data - Rebster - Oct-09-2020

(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.


RE: Next steps for using API data - buran - Oct-09-2020

(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?


RE: Next steps for using API data - Skaperen - Oct-10-2020

i had the same thought.  python can go directly to a many databases, skipping the CSV step, completely.


RE: Next steps for using API data - DPaul - Oct-10-2020

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


RE: Next steps for using API data - perfringo - Oct-10-2020

"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".