python - 使用字符串索引附加到 Python 字典时出现 KeyError
问题描述
我收到以下错误:
键错误:'sat'
在我的 Python 脚本中如下:
balls = {}
balls_count = 0
for entry_content in latest_results_soup.find_all('img',vspace='12'):
if balls_count < 5:
draw_day = 'sat'
else:
draw_day = 'wed'
balls[draw_day].append(int(entry_content['src'].rsplit('/', 1)[-1].split('.')[0]))
balls_count += 1
我正在寻找的是一个字典或列表来进一步解析,其结构类似于:
balls['sat'] = [5, 7, 20, 28, 30]
balls['wed'] = [1, 6, 9, 19, 20]
我觉得我很接近(Python n00b,顺便说一句),但显然不够接近。
解决方案
我认为您应该在设置值或附加值之前提供sat
and键。wed
balls = {'sat': [], 'wed': []}
推荐阅读
- python - python cursor.callproc() 函数不调用过程
- java - 在 Android 上获取当前位置
- jquery - jQuery 3.4.1 .on()“单击”选择器包含ID和类
- powerbi - Power BI - 从相关表中获取最新值
- python - PyMySQL 查询返回奇怪的字典
- r - R中每月的第二个到最后一个星期三
- javascript - Angular6 - 仅在 Safari 中出错 - TypeError undefined is not an object
- python - 使用字典查找和一些简单的数学创建 Dataframe 值
- java - 将 Java 与 OpenCV 和 Mask_RCNN 一起使用
- entity-framework-5 - 实体框架和 Oracle 的问题