美文网首页
Python markdown 表格转 excel

Python markdown 表格转 excel

作者: Rinaloving | 来源:发表于2024-08-05 15:46 被阅读0次

环境

  • 安装 padas
pip install pandas
  • 安装
 pip install openpyxl

代码

import pandas as pd
 
# 假设你的Markdown表格字符串如下
markdown_table = """
| Column1 | Column2 | Column3 |
|---------|---------|---------|
| Data1   | Data2   | Data3   |
| Data4   | Data5   | Data6   |
| Data7   | Data8   | Data9   |
"""
 
# 将Markdown表格字符串转换为pandas DataFrame
lines = markdown_table.strip().split('\n')
header = [cell.strip() for cell in lines[0].strip().split('|')[1:-1]]
data = [[cell.strip() for cell in row.strip().split('|')[1:-1]] for row in lines[1:-1]]
df = pd.DataFrame(data, columns=header)
 
# 将DataFrame保存到Excel文件
df.to_excel('output.xlsx', index=False)

效果

image.png
image.png

相关文章

网友评论

      本文标题:Python markdown 表格转 excel

      本文链接:https://www.haomeiwen.com/subject/tewxkjtx.html