ValueError:类型名称和字段名称必须是有效标识符:'Content-Type'

问题描述 投票:0回答:1

当我使用时

from allpairspy import AllPairs

parameters = OrderedDict([('Content-Type', ['"application/json"', ' "application/xml"'])]

test_cases = list(AllPairs(parameters))

它错误以下消息:

Traceback (most recent call last):
  File "/home/nio/Documents/projects/ironball/pairwise_engine/gen_pairwise_cases.py", line 60, in <module>
    for i, pairs in enumerate(AllPairs(parameters, filter_func=is_valid_combination)):
  File "/home/nio/miniconda3/envs/kytest/lib/python3.9/site-packages/allpairspy/allpairs.py", line 60, in __init__
    self.__pairs_class = namedtuple("Pairs", self.__param_name_list)
  File "/home/nio/miniconda3/envs/kytest/lib/python3.9/collections/__init__.py", line 390, in namedtuple
    raise ValueError('Type names and field names must be valid '
ValueError: Type names and field names must be valid identifiers: 'Content-Type'

当我将

'Content-Type'
更改为
'Content_Type'
时,它起作用了。但这里有什么问题呢?如何直接使用
'Content-Type'

linux python
1个回答
0
投票

引擎盖下有

namedtuple
,并且
typename
field_names
namedtuple
都必须是“有效标识符”。

如果字符串仅包含以下内容,则该字符串被视为有效标识符 字母数字字母 (a-z) 和 (0-9) 或下划线 (_)。一个有效的 标识符不能以数字开头,也不能包含任何空格。

要检查此情况

isidentifier
,请使用以下方法:

"Content-Type".isidentifier()  # False
"Content_Type".isidentifier()  # True

因此,在您的情况下,它返回

False
并命名元组引发错误。

© www.soinside.com 2019 - 2024. All rights reserved.