处理使用'http'下载的JSON时发生错误-TypeError:字符串索引必须为整数

Maciej

我正在尝试从API检索一些数据并将其放入数据库,但是我遇到了一些问题。

API数据:https//api.coronatracker.com/v4/analytics/newcases/country?countryCode = PL&startDate = 2020-11-01&endDate = 2020-11-06

来自上方网址的数据

[
  {"country":"Poland","country_code":"PL","last_updated":"2020-11-02T00:01:00.000Z","new_deaths":92,"new_infections":15578,"new_recovered":7818},
  {"country":"Poland","country_code":"PL","last_updated":"2020-11-03T00:02:00.000Z","new_deaths":227,"new_infections":19364,"new_recovered":5573},
  {"country":"Poland","country_code":"PL","last_updated":"2020-11-04T00:00:00.000Z","new_deaths":373,"new_infections":24692,"new_recovered":8974},
  {"country":"Poland","country_code":"PL","last_updated":"2020-11-05T00:11:00.000Z","new_deaths":367,"new_infections":27143,"new_recovered":8721}
]

错误

日期= i ['last_updated'] TypeError:字符串索引必须为整数

我的代码的一部分

try:
    response = http.request('GET', url)
    data = json.loads(response.data.decode('utf-8'))
    index = 0

    for i in data:
        Date = None
        Confirmed = None
        Deaths = None
        Recovered = None
        #Active = None

        Date = i['last_updated']
        Confirmed = i['new_infections']
        Deaths = i['new_deaths']
        Recovered = i['new_recovered']
        #Active = i['Active']

        cur.execute("""
            INSERT INTO covid_stats_poland
            VALUES (%s, %s, %s, %s, %s); 
            """,
            (index, Date, Confirmed, Deaths, Recovered))
        conn.commit()
        index += 1
    cur.close()

有人可以解释我在做什么错/如何解决?

克里斯托弗·佩塞雷(Christopher Peiseret)

该错误最有可能是由于data包含其他HTTP信息(例如标头)。请参阅以下有关检索JSON的两种标准方法。

选项1: urllib.request

import json
import urllib.request 

URL = "https://api.coronatracker.com/v4/analytics/newcases/country?countryCode=PL&startDate=2020-11-01&endDate=2020-11-06"

with urllib.request.urlopen(URL) as url:
    data = json.loads(url.read().decode())
    print(data)

for i in data:
    Date = i['last_updated']
    Confirmed = i['new_infections']
    Deaths = i['new_deaths']
    Recovered = i['new_recovered']

    print(f"\nDate: {Date}")
    print(f"Confirmed: {Confirmed}")
    print(f"Deaths: {Deaths}")
    print(f"Recovered: {Recovered}")

输出量

[{'country': 'Poland', 'country_code': 'PL', 'last_updated': '2020-11-02T00:01:00.000Z', 'new_deaths': 92, 'new_infections': 15578, 'new_recovered': 7818}, {'country': 'Poland', 'country_code': 'PL', 'last_updated': '2020-11-03T00:02:00.000Z', 'new_deaths': 227, 'new_infections': 19364, 'new_recovered': 5573}, {'country': 'Poland', 'country_code': 'PL', 'last_updated': '2020-11-04T00:00:00.000Z', 'new_deaths': 373, 'new_infections': 24692, 'new_recovered': 8974}, {'country': 'Poland', 'country_code': 'PL', 'last_updated': '2020-11-05T00:11:00.000Z', 'new_deaths': 367, 'new_infections': 27143, 'new_recovered': 8721}]

Date: 2020-11-02T00:01:00.000Z
Confirmed: 15578
Deaths: 92
Recovered: 7818

Date: 2020-11-03T00:02:00.000Z
Confirmed: 19364
Deaths: 227
Recovered: 5573

Date: 2020-11-04T00:00:00.000Z
Confirmed: 24692
Deaths: 373
Recovered: 8974

Date: 2020-11-05T00:11:00.000Z
Confirmed: 27143
Deaths: 367
Recovered: 8721

选项2:http模块(较不常见)

import http
import json

URL = "api.coronatracker.com"
url_path = "/v4/analytics/newcases/country?countryCode=PL&startDate=2020-11-01&endDate=2020-11-06"

conn = http.client.HTTPSConnection(URL)
conn.request("GET", url_path)
response = conn.getresponse()
data = json.loads(response.read().decode())
print(data)

for i in data:
    Date = i['last_updated']
    Confirmed = i['new_infections']
    Deaths = i['new_deaths']
    Recovered = i['new_recovered']

    print(f"\nDate: {Date}")
    print(f"Confirmed: {Confirmed}")
    print(f"Deaths: {Deaths}")
    print(f"Recovered: {Recovered}")

输出量

[{'country': 'Poland', 'country_code': 'PL', 'last_updated': '2020-11-02T00:01:00.000Z', 'new_deaths': 92, 'new_infections': 15578, 'new_recovered': 7818}, {'country': 'Poland', 'country_code': 'PL', 'last_updated': '2020-11-03T00:02:00.000Z', 'new_deaths': 227, 'new_infections': 19364, 'new_recovered': 5573}, {'country': 'Poland', 'country_code': 'PL', 'last_updated': '2020-11-04T00:00:00.000Z', 'new_deaths': 373, 'new_infections': 24692, 'new_recovered': 8974}, {'country': 'Poland', 'country_code': 'PL', 'last_updated': '2020-11-05T00:11:00.000Z', 'new_deaths': 367, 'new_infections': 27143, 'new_recovered': 8721}]

Date: 2020-11-02T00:01:00.000Z
Confirmed: 15578
Deaths: 92
Recovered: 7818

Date: 2020-11-03T00:02:00.000Z
Confirmed: 19364
Deaths: 227
Recovered: 5573

Date: 2020-11-04T00:00:00.000Z
Confirmed: 24692
Deaths: 373
Recovered: 8974

Date: 2020-11-05T00:11:00.000Z
Confirmed: 27143
Deaths: 367
Recovered: 8721

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用cleaned_data时如何解决“ TypeError:字符串索引必须为整数”?

TypeError:使用BeautifulSoup获取跨度标签类时,字符串索引必须为整数

TypeError:使用Python解析Json时,字符串索引必须是整数错误

使用tensorflow create_coco_tf_record脚本时出错:“ TypeError:字符串索引必须为整数”

TypeError:使用Python解析JSON时,字符串索引必须是整数?

类型错误:处理 json 文件时字符串索引必须是整数

json数据处理错误“字符串索引必须是整数”

TypeError:当我尝试从JSON获取值时,字符串索引必须为整数

使用Python和GitHub的API:TypeError:字符串索引必须为整数

如何解决“ TypeError:字符串索引必须为整数”错误?

为什么会出现错误“ TypeError:字符串索引必须为整数”

TypeError:尝试打印href时,字符串索引必须为整数

TypeError字符串索引必须为整数-python json dict

如何解决此JSON TypeError:字符串索引必须为整数?

类型错误:使用带有字符串参数的 itemgetter 时,字符串索引必须是整数

TypeError:字符串索引必须是整数-json

TypeError:字符串索引必须是整数 JSON 文件

json.loads TypeError:字符串索引必须是整数

JSON:TypeError: 字符串索引必须是整数

Python - 读取 JSON - TypeError:字符串索引必须是整数

使用 json 数据解析错误:字符串索引必须是整数

具有子字符串的API JSON字典返回TypeError:字符串索引必须为整数

JSON字符串索引必须为整数

读取JSON字符串| TypeError:字符串索引必须是整数

错误:字符串索引必须是整数

Python 3.6 TypeError:尝试发送Office 365电子邮件时,字符串索引必须为整数

TypeScript:尝试使用字符串|时,索引签名参数必须为“字符串”或“数字” 数

我的代码正在处理字典列表,例如字符串,typeerror:TypeError:字符串索引必须是整数

TypeError:字符串索引必须是jSON和Flask的整数错误