gpt4 book ai didi

python - 如何在配置解析器 python 中存储和检索元组字典?

转载 作者:行者123 更新时间:2023-12-01 07:36:35 24 4
gpt4 key购买 nike

我正在使用configparser python 中的库来管理我的配置文件。

但是,我找不到通过它存储和检索元组数据结构的方法。

我的数据是元组字典。

name_mapper = {
0 = (0, 0)
1 = (0, 1)
2 = (0, 2)
3 = (1, 0)
4 = (1, 1)
5 = (1, 2)
6 = (2, 0)
7 = (2, 1)
8 = (2, 2)
9 = (3, 0)
10 = (3, 1)
11 = (3, 2)
}

当我通过 configparser 编写这本字典时,所有内容都变成了字符串。

myconfig.ini

[NAME_MAPPER]
0 = (0, 0)
1 = (0, 1)
2 = (0, 2)
3 = (1, 0)
4 = (1, 1)
5 = (1, 2)
6 = (2, 0)
7 = (2, 1)
8 = (2, 2)
9 = (3, 0)
10 = (3, 1)
11 = (3, 2)

现在,阅读“my_config.ini”

config = configparser.ConfigParser()
config.read('config_params.ini')
name_mapper = dict(config['NAME_MAPPER'])

但是,字典不再保存元组,它只是格式化为字符串的元组。

name_mapper = {
'0' = '(0, 0)'
'1' = '(0, 1)'
'2' = '(0, 2)'
'3' = '(1, 0)'
'4' = '(1, 1)'
'5' = '(1, 2)'
'6' = '(2, 0)'
'7' = '(2, 1)'
'8' = '(2, 2)'
'9' = '(3, 0)'
'10' = '(3, 1)'
'11' = '(3, 2)'
}

我找到了一种使用 ast.literal_eval 方法来纠正此问题的方法。

from ast import literal_eval

new_name_mapper = dict()
for each in name_mapper:
new_name_mapper[int(each)] = literal_eval(name_mapper[each])

现在,new_name_mapper 的格式已正确。

name_mapper = {
0 = (0, 0)
1 = (0, 1)
2 = (0, 2)
3 = (1, 0)
4 = (1, 1)
5 = (1, 2)
6 = (2, 0)
7 = (2, 1)
8 = (2, 2)
9 = (3, 0)
10 = (3, 1)
11 = (3, 2)
}

但我确信,这不是最好的方法。任何人都有更好、更多Python风格的想法。

最佳答案

配置解析器将始终返回字符串,除非您使用使用 getbool getint`` 等方法的显式转换器。但是,您可以编写自己的转换器函数并将其注册到解析器,然后使用它以您喜欢的任何方式检索值。

来自Configparser Docs :

converters, default value: not set

Config parsers provide option value getters that perform type conversion. By default getint(), getfloat(), and getboolean() are implemented. Should other getters be desirable, users may define them in a subclass or pass a dictionary where each key is a name of the converter and each value is a callable implementing said conversion. For instance, passing {'decimal': decimal.Decimal} would add getdecimal() on both the parser object and all section proxies. In other words, it will be possible to write both parser_instance.getdecimal('section', 'key', fallback=0) and parser_instance['section'].getdecimal('key', 0).

If the converter needs to access the state of the parser, it can be implemented as a method on a config parser subclass. If the name of this method starts with get, it will be available on all section proxies, in the dict-compatible form (see the getdecimal() example above).

所以你可以编写一个函数来解析元组

 >>> def parse_tuple(input):
... return tuple(k.strip() for k in input[1:-1].split(','))

>>> parse_tuple('(1, 2, 3)')
>>> ('1', '2', '3')

或者如果你想要整数:

 >>> def parse_int_tuple(input):
... return tuple(int(k.strip()) for k in input[1:-1].split(','))

>>> parse_int_tuple('(1, 2, 3)')
>>> (1, 2, 3)

创建您的 configparser 对象,传递此转换器:

>>> parser = ConfigParser(converters={'tuple': parse_int_tuple})
>>> parser.read_dict({'NAME_MAPPER': name_mapper})
>>> parser['NAME_MAPPER'].gettuple('0')
(0, 0)

在我看来,这是最Pythonic的方法,因为它只使用有据可查的功能

关于python - 如何在配置解析器 python 中存储和检索元组字典?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56967754/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com