You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Python中高效将Markdown表格转换为DataFrame?

问题:将Markdown表格优雅转换为pandas DataFrame

我需要将Markdown表格转换为pandas DataFrame,目前已使用pd.read_csv函数并以'|'作为分隔符实现转换,但仍需额外清理操作:移除用于分隔表格的'-----'行,同时删除最后一列。以下是我当前的简化实现代码:

import pandas as pd
from io import StringIO

# The text containing the table
text = """
| 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      |
"""

# Use StringIO to create a file-like object from the text
text_file = StringIO(text)

# Read the table using pandas read_csv with '|' as the separator
df = pd.read_csv(text_file, sep='|', skipinitialspace=True)

# Remove leading/trailing whitespace from column names
df.columns = df.columns.str.strip()

# Remove the index column
df = df.iloc[:, 1:]

请问是否存在无需额外清理步骤、更优雅高效的转换方法?希望得到相关建议与改进思路。


改进方案与思路

可以通过充分利用pandas.read_csv的内置参数,减少后续手动清理操作,实现更简洁的转换:

1. 内置参数一步到位

通过skiprows跳过分隔线行,usecols过滤无效空列,同时在读取时直接处理列名空格:

import pandas as pd
from io import StringIO

text = """
| 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      |
"""

text_file = StringIO(text)
df = pd.read_csv(
    text_file,
    sep='|',
    skipinitialspace=True,
    skiprows=[1],  # 跳过第二行的分隔线
    header=0,
    usecols=[1, 2, 3],  # 排除首尾因Markdown格式产生的空列
    names=[col.strip() for col in pd.read_csv(StringIO(text), sep='|', nrows=0).columns[1:-1]]
)

2. 利用comment参数自动跳过分隔线

Markdown表格的分隔线行以-开头,用comment='-'可以自动跳过这类行,再配合usecols过滤空列,最后一次性清理列名:

import pandas as pd
from io import StringIO

text = """
| 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      |
"""

text_file = StringIO(text)
df = pd.read_csv(
    text_file,
    sep='|',
    skipinitialspace=True,
    comment='-',  # 跳过所有以'-'开头的行
    usecols=lambda col: col.strip() != ''  # 过滤空列
)
df.columns = df.columns.str.strip()

3. 第三方库简化操作(可选)

如果频繁处理Markdown表格,使用专门的第三方库可以彻底省去格式处理的麻烦,比如markdown-table-to-dataframe:

首先安装库:

pip install markdown-table-to-dataframe

然后直接转换:

from markdown_table_to_dataframe import markdown_table_to_dataframe

text = """
| 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      |
"""

df = markdown_table_to_dataframe(text)

内容的提问来源于stack exchange,提问作者Kilian Shiliao

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.11 03:35:27