爬取疫情数据,爬取日期时报错

爬取疫情数据,爬取日期时报错

for i in data_all["chinaDayList"]:
        ds = i["date"]
        #ds=json.loads(ds)
        tup = time.strptime(ds,"%Y-%m-%d")
        ds = time.strftime("%Y-%m-%d",tup)#改变时间格式,不然插入数据库会出错,数据库是datetime类型
        confirm = i["total"]["confirm"]
        suspect = i["total"]["suspect"]
        heal = i["total"]["heal"]
        dead = i["total"]["dead"]
        history[ds] = {"confirm":confirm,"suspect":suspect,"heal":heal,"dead":dead}






在我运行函数时,报出以下错误


 ds = i["date"]
TypeError: string indices must be integers

chinaDaylist里的数据截取,我现在就想得到date里的数据,求解答!

{'date': 2020-01-20,
 'today': {'confirm': 291, 'suspect': 27, 'heal': 25, 'dead': 6, 'severe': 0, 'storeConfirm': None}, 
'total': {'confirm': 291, 'suspect': 54, 'heal': 25, 'dead': 6, 'severe': 0, 'input': 0}, 
'extData': None, 
'lastUpdateTime': None}

我将str类型的字符串ds转换成dict,转换不了,但我将ds输出可以输出日期,放进函数就报错

如果你试图写 i['date'] ,那么它转换成 'date'['date'] ,这在 Python 中是没有意义的。

解决这个问题的方法是引用字典 chinadaylist(用字典,就是from i in 后面的,我也不确定你后面定位写的对不对),而不是使用i 。

不知道你这个问题是否已经解决, 如果还没有解决的话:

如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^