如何从API中的steaming csv数据创建一个dict对象?

时间:2017-01-24 05:36:29

标签: python csv python-requests etl

我需要从API端点读取一些数据(由于隐私问题我不能在这里分享),其中包含第一行中的标题和来自此处的数据行。所以,仅举例来说,它可能看起来像:

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

我需要从此API读取数据并将其加载到已存在的数据库中。我这样读了数据:

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], ...)

在实际案例中,我添加了将数据加载到数据库而不是print()的代码。由于API中的数据与数据库列的顺序不同,我希望能够按名称引用每行的列,就像dict那样line['date'], line['field1'] etc.

我该怎么做?

0 个答案:

没有答案