0

I need to read some data from an API endpoint (which I cannot share here because of privacy issues), that contains the header in the very first row and data rows from there on. So, just for examples sake it may look like:

id    date         field1    field2   .... field100
1     2017-01-22    1.0       a              5
2     2017-01-22    2.5       b              25
3     2017-01-19    2.0       a              12
4     2017-01-19    3.5       c              3
5     2017-01-14    5.0       e              4
6     2017-01-14    6.5       f              9
7     2017-01-10    4.5       g              8
...

I need to read the data from this API and load it into a database that already exists. I read the data like so:

import requests

data = requests.get(API_URL, stream=True)
data.raise_for_status()

for line in data.iter_lines():
    if line:
        print(line[0], line[1], ...)

I'd add code to load the data into database instead of print() here in the real case. Since the data in the API aren't in the same order as the database columns I want to be able to reference the columns for each row by name like a dict does so something like line['date'], line['field1'] etc.

How do I do that?

sfactor
  • 12,592
  • 32
  • 102
  • 152

0 Answers0