Python Forum
Next steps for using API data
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Next steps for using API data
#1
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.
Reply
#2
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.
Reply
#3
(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.
Reply
#4
(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?
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
i had the same thought.  python can go directly to a many databases, skipping the CSV step, completely.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#6
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
Reply
#7
"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".
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Thumbs Up Sorting Steps MoreMoney 19 867 Mar-31-2024, 10:59 AM
Last Post: Pedroski55
  Keep Toggle View through Slider Steps yourboyjoe 1 1,682 Aug-10-2020, 07:32 PM
Last Post: Yoriz
  Files to store configuration and steps for a industrial process control application Danieru 1 1,801 Aug-03-2020, 05:43 PM
Last Post: buran
  Pexpect baby steps slouw 9 7,953 May-23-2019, 10:21 AM
Last Post: heiner55
  first steps with python3 hunnimonstr 5 4,568 Jul-03-2017, 08:49 PM
Last Post: hunnimonstr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020