python - Python: Convert markdown table to json with
问题描述
I am trying to figure out, what is the easiest way to convert some markdown table text into json using only python. For example, consider this as input string:
| Some Title | Some Description | Some Number |
|------------|------------------------------|-------------|
| Dark Souls | This is a fun game | 5 |
| Bloodborne | This one is even better | 2 |
| Sekiro | This one is also pretty good | 110101 |
The wanted output should be this:
[
{"Some Title":"Dark Souls","Some Description":"This is a fun game","Some Number":5},
{"Some Title":"Bloodborne","Some Description":"This one is even better","Some Number":2},
{"Some Title":"Sekiro","Some Description":"This one is also pretty good","Some Number":110101}
]
Note: Ideally, the output should be RFC 8259 compliant, aka use double quotes " instead of single quotes ' around they key value pairs.
I've seen some JS libraries that do that, but nothing for python only. Can someone explain to me whats the quickest way to achieve this, so I don't have to write my own parser for this.
All help is appreciated!
解决方案
My approach was very similar to @Kuldeep Singh Sidhu's:
md_table = """
| Some Title | Some Description | Some Number |
|------------|------------------------------|-------------|
| Dark Souls | This is a fun game | 5 |
| Bloodborne | This one is even better | 2 |
| Sekiro | This one is also pretty good | 110101 |
"""
result = []
for n, line in enumerate(md_table[1:-1].split('\n')):
data = {}
if n == 0:
header = [t.strip() for t in line.split('|')[1:-1]]
if n > 1:
values = [t.strip() for t in line.split('|')[1:-1]]
for col, value in zip(header, values):
data[col] = value
result.append(data)
Result is:
[{'Some Title': 'Dark Souls',
'Some Description': 'This is a fun game',
'Some Number': '5'},
{'Some Title': 'Bloodborne',
'Some Description': 'This one is even better',
'Some Number': '2'},
{'Some Title': 'Sekiro',
'Some Description': 'This one is also pretty good',
'Some Number': '110101'}]
推荐阅读
- reactjs - XHR 请求在页面加载时表现不佳,但之后更快
- matlab - 在Matlab中找到许多半空间的交集
- intellij-idea - 如何调试基于 groovy 的插件并使用 IntelliJ 进入其源代码?
- python - 创建多个熊猫字符串
- javascript - 尝试使用带有指向不同部分的多个锚链接的 smooth-scrollbar.js
- css - 在前后显示一条带点的线
- powershell - 在 Wix 中隐藏执行 PowerShell
- python - plotly-dash Choropleth mapbox 图中的选择行为
- html - 尝试在css中制作磨砂玻璃效果
- css - 媒体查询在 Firefox 上的错误点触发