我如何将 Markdown 列表读入 Python OrderedDict?
How could I read a Markdown list into a Python OrderedDict?
我有以下形式的 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')]))]))])
打印时看起来不太好看,但功能正常。
我有以下形式的 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')]))]))])
打印时看起来不太好看,但功能正常。