Programmatically convert pandas dataframe to markdown table

后端 未结 13 1268
梦毁少年i
梦毁少年i 2020-12-13 03:44

I have a Pandas Dataframe generated from a database, which has data with mixed encodings. For example:

+----+-------------------------+----------+-----------         


        
13条回答
  •  抹茶落季
    2020-12-13 04:40

    Using external tool pandoc and pipe:

    def to_markdown(df):
        from subprocess import Popen, PIPE
        s = df.to_latex()
        p = Popen('pandoc -f latex -t markdown',
                  stdin=PIPE, stdout=PIPE, shell=True)
        stdoutdata, _ = p.communicate(input=s.encode("utf-8"))
        return stdoutdata.decode("utf-8")
    

提交回复
热议问题