我们使用的库是markdown_strings
pip install markdown_strings
程序如下:
from markdown_strings import header, table, code_block
with open("mark_down.md", 'w', encoding="utf8") as file:
file.write(header("一级目录", 1) + "\n")
file.write("这是一个文本\n")
file.write(code_block("import pandas as pd\npd.DataFrame()", "python") + "\n")
file.write(table([["列1", "1-1", "1-2", "1-3"],
["列2", "2-1", "2-2", "2-3"],
["列3", "3-1", "3-2", "3-3"],
["列4", "4-1", "4-2", "4-3"]]))
得到输出结果,效果图如下: