用于项目的API端点返回表单的纯文本响应:
[RESPONSE]
code = 200
description = Command completed successfully
queuetime = 0
runtime = 0.071
property[abuse policy][0] = The policies are published at the REGISTRY_OPERATOR website at:
property[abuse policy][1] = =>https://registry.in/Policies
property[abuse policy][2] =
property[abuse policy][3] = IN Policy Framework: https://registry.in/system/files/inpolicy_0.pdf
property[abuse policy][4] = IN Domain Anti-Abuse policy: https://registry.in/Policies/IN_Anti_Abuse_Policy
property[abuse policy url][0] = https://registry.in/Policies/IN_Anti_Abuse_Policy
property[active][0] = 0我正在尝试使用Python将其解析为字典。目前,我有以下代码:
import re
def text_to_dict(text):
js = {}
for s in text.splitlines():
x = s.split("=", maxsplit=1)
if len(x) > 1:
keys = [k for i in re.split("\]|\[", x[0]) if (k := i.strip())]
for i, k in enumerate(keys):
pd = js
for j,pk in enumerate(keys[:i]):
if keys[j+1:j+2] and not (keys[j+1:j+2][0]).isnumeric():
pd = pd[pk]
if k not in pd:
if k.isnumeric():
pd[keys[i-1]].append((x[1]).strip())
else:
pd[k] = (x[1]).strip() if i == len(keys)-1 else [] if keys[i+1:i+2] and (keys[i+1:i+2][0]).isnumeric() else {}
return js此代码可以处理上面的示例,并返回:
{
"code": "200",
"description": "Command completed successfully",
"runtime": "0.081",
"queuetime": "0",
"property": {
"abuse policy": [
"The policies are published at the REGISTRY_OPERATOR website at:",
"=>https://registry.in/Policies",
"",
"IN Policy Framework: https://registry.in/system/files/inpolicy_0.pdf",
"IN Domain Anti-Abuse policy: https://registry.in/Policies/IN_Anti_Abuse_Policy"
],
"abuse policy url": [
"https://registry.in/Policies/IN_Anti_Abuse_Policy"
],
"active": [
"0"
]
}
}但是,如果我将其附加到上面的示例中,则无法处理以下问题:
...
property[active][1][test] = TEST或
...
property[active][1][0] = TEST哪一个应该回来
{
...
"active": [
"0",
{"test": "TEST"}
]
}和
{
...
"active": [
"0",
["TEST"]
]
}分别使用。
我觉得有一种更简单的方法来解释所有的可能性,而不需要编写一堆嵌套的ifs,但我不知道是什么。
发布于 2022-03-18 05:49:27
您的输入数据实际上是INI文件格式。为了方便起见,Python使用了configparser模块。
当我们假定密钥'property[foo][0][test]'的每一部分实际上都是一个dict (没有嵌套列表)时,我们会将其解析为这个结构:
{'property': {'foo': {'0': {'test': 'value'}}}}这可以通过一个循环来完成,该循环一直在创建嵌套的dicts:
from configparser import ConfigParser
def parse(text):
config = ConfigParser()
config.read_string(text)
root = {}
for key in config['RESPONSE'].keys():
curr = root
for key_part in key.replace(']', '').split('['):
if key_part not in curr:
curr[key_part] = {}
prev = curr
curr = curr[key_part]
prev[key_part] = config['RESPONSE'][key]
return root用法
from pprint import pprint
text = """
[RESPONSE]
code = 200
description = Command completed successfully
queuetime = 0
runtime = 0.071
property[abuse policy][0] = The policies are published at the REGISTRY_OPERATOR website at:
property[abuse policy][1] = =>https://registry.in/Policies
property[abuse policy][2] =
property[abuse policy][3] = IN Policy Framework: https://registry.in/system/files/inpolicy_0.pdf
property[abuse policy][4] = IN Domain Anti-Abuse policy: https://registry.in/Policies/IN_Anti_Abuse_Policy
property[abuse policy url][0] = https://registry.in/Policies/IN_Anti_Abuse_Policy
property[active][0] = 0
property[foo][0][test] = a
property[foo][1][test] = b
property[bar][0][0] = A
property[bar][1][1] = B
"""
pprint(parse(text))结果
{'code': '200',
'description': 'Command completed successfully',
'property': {'abuse policy': {'0': 'The policies are published at the '
'REGISTRY_OPERATOR website at:',
'1': '=>https://registry.in/Policies',
'2': '',
'3': 'IN Policy Framework: '
'https://registry.in/system/files/inpolicy_0.pdf',
'4': 'IN Domain Anti-Abuse policy: '
'https://registry.in/Policies/IN_Anti_Abuse_Policy'},
'abuse policy url': {'0': 'https://registry.in/Policies/IN_Anti_Abuse_Policy'},
'active': {'0': '0'},
'bar': {'0': {'0': 'A'}, '1': {'1': 'B'}},
'foo': {'0': {'test': 'a'}, '1': {'test': 'b'}}},
'queuetime': '0',
'runtime': '0.071'}您可以检查key_part是否是数字的,并将其转换为int,以便生成的结构更像包含的列表。
{'property': {'foo': {0: {'test': 'value'}}}}https://stackoverflow.com/questions/71522248
复制相似问题