首页 > 解决方案 > 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!

标签: pythonparsingmarkdown

解决方案


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'}]

推荐阅读