gpt4 book ai didi

python - 如何将 Markdown 列表读入 Python OrderedDict?

转载 作者:太空宇宙 更新时间:2023-11-03 17:11:49 24 4
gpt4 key购买 nike

我有以下形式的 Markdown 列表:

- launchers
- say hello
- command: echo "hello" | festival --tts
- icon: shebang.svg
- say world
- command: echo "world" | festival --tts
- icon: shebang.svg
- say date
- command: date | festival --tts

我有一个函数可以将此 Markdown 列表转换为字典,如下所示:

{'say world': {'command': 'echo "world" | festival --tts', 'icon': 'shebang.svg'}, 'say hello': {'command': 'echo "hello" | festival --tts', 'icon': 'shebang.svg'}, 'say date': {'command': 'date | festival --tts'}}

当我这样做时,显然顺序丢失了。保持这种顺序的适当方法是什么?一个简单的列表会好吗? OrderedDict 会更好吗?应该怎么做?

到目前为止,我所拥有的内容如下所示,作为一个最小的工作示例:

import re

def Markdown_list_to_dictionary(Markdown_list):
line = re.compile(r"( *)- ([^:\n]+)(?:: ([^\n]*))?\n?")
depth = 0
stack = [{}]
for indent, name, value in line.findall(Markdown_list):
indent = len(indent)
if indent > depth:
assert not stack[-1], "unexpected indent"
elif indent < depth:
stack.pop()
stack[-1][name] = value or {}
if not value:
# new branch
stack.append(stack[-1][name])
depth = indent
return(stack[0])

Markdown_list =\
"""
- launchers
- say hello
- command: echo "hello" | festival --tts
- icon: shebang.svg
- say world
- command: echo "world" | festival --tts
- icon: shebang.svg
- say date
- command: date | festival --tts
"""

print(Markdown_list_to_dictionary(Markdown_list))

最佳答案

是的,OrderedDict 看起来应该在这种情况下工作。您的代码将如下所示:

import re
from collections import OrderedDict as _OrderedDict

def Markdown_list_to_dictionary(Markdown_list):
line = re.compile(r"( *)- ([^:\n]+)(?:: ([^\n]*))?\n?")
depth = 0
stack = [_OrderedDict()]
for indent, name, value in line.findall(Markdown_list):
indent = len(indent)
if indent > depth:
assert not stack[-1], "unexpected indent"
elif indent < depth:
stack.pop()
stack[-1][name] = value or _OrderedDict()
if not value:
# new branch
stack.append(stack[-1][name])
depth = indent
return(stack[0])

Markdown_list =\
"""
- launchers
- say hello
- command: echo "hello" | festival --tts
- icon: shebang.svg
- say world
- command: echo "world" | festival --tts
- icon: shebang.svg
- say date
- command: date | festival --tts
"""

print(Markdown_list_to_dictionary(Markdown_list))

输出如下:

OrderedDict([('launchers', OrderedDict([('say hello', OrderedDict([('command', 'echo "hello" | festival --tts'), ('icon', 'shebang.svg')])), ('say world', OrderedDict([('command', 'echo "world" | festival --tts'), ('icon', 'shebang.svg')])), ('say date', OrderedDict([('command', 'date | festival --tts')]))]))])

打印时看起来不太好看,但它确实可以正常工作。

关于python - 如何将 Markdown 列表读入 Python OrderedDict?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34003793/

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