![](/img/trans.png)
[英]Unsupported operand type(s) for *: 'NoneType' and 'NoneType' (Python)
[英]Python error with literals and lists in loguru: unsupported operand type(s) for +: 'NoneType' and 'list'
使用 loguru 将列表写回配置文件,并收到可怕unsupported operand type(s) for +: 'NoneType' and 'list'
错误。 我知道错误来自尝试结合文字和列表。 我试图通过使用变量来表示文字来分解该行,但这只会产生相同的错误。
def update_config(config: dict):
"""
Update exisitng configuration file.
Parameters
----------
config: dict
Configuraiton to be written into config file.
"""
config["ids_to_retry"] = list(set(config["ids_to_retry"] + FAILED_IDS))
with open("config.yaml", "w") as yaml_file:
yaml.dump(config, yaml_file, default_flow_style=False)
logger.info("Updated last read message_id to config file")
Traceback (most recent call last):
File "/telegram-downloader-quackified/media_downloader.py", line 359, in <module>
main()
File "/telegram-downloader-quackified/media_downloader.py", line 343, in main
updated_config = asyncio.get_event_loop().run_until_complete(
File "/miniconda3/lib/python3.9/asyncio/base_events.py", line 642, in run_until_complete
return future.result()
File "/telegram-downloader-quackified/media_downloader.py", line 324, in begin_import
update_config(config)
File "/telegram-downloader-quackified/media_downloader.py", line 35, in update_config
config["ids_to_retry"] = list(set(config["ids_to_retry"] + FAILED_IDS))
我知道短语list(set(config["ids_to_retry"] + FAILED_IDS))
中需要更改一些内容,我只是不确定是什么。
FAILED_IDS 是脚本遇到异常无法成功完成下载时生成的列表。 发生这种情况时,消息 ID 将存储在 memory 中,就像FAILED_IDS.append(message.message_id)
一样。 然后使用以下语句将其写入配置文件:
if FAILED_IDS:
logger.info(
"Downloading of %d files failed. "
"Failed message ids are added to config file.\n",
len(set(FAILED_IDS)),
)
update_config(updated_config)
它的原始值是:
FAILED_IDS: list = []
其中“ids_to_retry”或多或少只是配置文件中使用的 label。 它只是一个文字字符串,并且是非类型。 最终结果是将两者写入配置文件中,类似于以下内容:
ids_to_retry:
- 26125
- 2063
- 2065
- 2080
- 2081
- 22052
- 21029
- 553
- 554
- 555
- 2102
- 22074
我希望这能阐明我们正在使用的变量的性质。
失败的原因是config["ids_to_retry"] = None
并且您正在尝试将 Nonetype 扩展到列表。 你可以使用类似的东西
config["ids_to_retry"] = list(set(config["ids_to_retry"] + FAILED_IDS)) if config["ids_to_retry"] else FAILED_IDS
因此,如果config["ids_to_retry"]
为None
,则需要config["ids_to_retry"] = FAILED_IDS
,而无需尝试扩展它。
假设FAILED_IDS
是一个列表,如果不是,您可以将其强制转换为列表。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.