简体   繁体   English

Python loguru中的文字和列表错误:+的不支持的操作数类型:'NoneType'和'list'

[英]Python error with literals and lists in loguru: unsupported operand type(s) for +: 'NoneType' and 'list'

Working with loguru in order to write a list back to the configuration file, and received the dreaded unsupported operand type(s) for +: 'NoneType' and 'list' error.使用 loguru 将列表写回配置文件,并收到可怕unsupported operand type(s) for +: 'NoneType' and 'list'错误。 I know the error came from attempting to combine a literal and a list.我知道错误来自尝试结合文字和列表。 I attempted to break down the line by using a variable to represent the literal, and this only generated the same error.我试图通过使用变量来表示文字来分解该行,但这只会产生相同的错误。

Block of code with problematic line:有问题的代码块:

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")

Full error output:完全错误 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))

I know something needs to change in the phrase list(set(config["ids_to_retry"] + FAILED_IDS)) , I just am not sure what.我知道短语list(set(config["ids_to_retry"] + FAILED_IDS))中需要更改一些内容,我只是不确定是什么。


Providing more info:提供更多信息:

FAILED_IDS is a list generated when the script encounters an exception and cannot successfully complete the download. FAILED_IDS 是脚本遇到异常无法成功完成下载时生成的列表。 When this occurs the message id is stored in memory like so FAILED_IDS.append(message.message_id) .发生这种情况时,消息 ID 将存储在 memory 中,就像FAILED_IDS.append(message.message_id)一样。 Then it is written to the configuration file with the following statement:然后使用以下语句将其写入配置文件:

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)

It's original value is:它的原始值是:

FAILED_IDS: list = []

Where "ids_to_retry" is more or less just a label used in the configuration file.其中“ids_to_retry”或多或少只是配置文件中使用的 label。 It is just a literal string, and is the nonetype.它只是一个文字字符串,并且是非类型。 The ends result is for the two to be written in the configuration file to resemble something like the following:最终结果是将两者写入配置文件中,类似于以下内容:

ids_to_retry:
- 26125
- 2063
- 2065
- 2080
- 2081
- 22052
- 21029
- 553
- 554
- 555
- 2102
- 22074

I hope this clarifies the nature of the variables we are working with.我希望这能阐明我们正在使用的变量的性质。

The reason for failure is config["ids_to_retry"] = None and you are trying to extend a Nonetype to a list.失败的原因是config["ids_to_retry"] = None并且您正在尝试将 Nonetype 扩展到列表。 You can use something like你可以使用类似的东西

config["ids_to_retry"] = list(set(config["ids_to_retry"] + FAILED_IDS)) if config["ids_to_retry"] else FAILED_IDS

So if config["ids_to_retry"] is None it would take config["ids_to_retry"] = FAILED_IDS , Without trying to extending it.因此,如果config["ids_to_retry"]None ,则需要config["ids_to_retry"] = FAILED_IDS ,而无需尝试扩展它。

Assumption made that FAILED_IDS is a list if not you can type cast it to a list.假设FAILED_IDS是一个列表,如果不是,您可以将其强制转换为列表。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 *:&#39;NoneType&#39;和&#39;NoneType&#39;(Python)不受支持的操作数类型 - Unsupported operand type(s) for *: 'NoneType' and 'NoneType' (Python) Python错误-TypeError:+:&#39;NoneType&#39;和&#39;str&#39;不支持的操作数类型 - Python error - TypeError: unsupported operand type(s) for +: 'NoneType' and 'str' + =不支持的操作数类型:“ NoneType”和“ list” - unsupported operand type(s) for +=: 'NoneType' and 'list' %的Python错误不支持的操作数类型:我的代码为“ NoneType”和“ str” - Python Error unsupported operand type(s) for %: 'NoneType' and 'str' on my code python中的错误:*:&#39;int&#39;和&#39;NoneType&#39;不受支持的操作数类型 - Error in python: unsupported operand type(s) for *: 'int' and 'NoneType' Python错误:-:&#39;float&#39;和&#39;NoneType&#39;不受支持的操作数类型 - Python error: unsupported operand type(s) for -: 'float' and 'NoneType' Python 错误:+ 不支持的操作数类型:&#39;int&#39; 和 &#39;NoneType&#39; - Python Error: unsupported operand type(s) for +: 'int' and 'NoneType' 错误:/不支持的操作数类型:“ NoneType”和“ int” - Error: unsupported operand type(s) for /: 'NoneType' and 'int' Python:使用lambda时,&:&#39;NoneType&#39;和&#39;NoneType&#39;不受支持的操作数类型 - Python: Unsupported operand type(s) for &: 'NoneType' and 'NoneType' whilst using lambda python函数处理给出错误TypeError:不支持的操作数类型-:&#39;NoneType&#39;和&#39;NoneType&#39; - python function handling giving error TypeError: unsupported operand type(s) for -: 'NoneType' and 'NoneType'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM