簡體   English   中英

Python loguru中的文字和列表錯誤:+的不支持的操作數類型:'NoneType'和'list'

[英]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")

完全錯誤 output:

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM