Write to StringIO object using Pandas Excelwriter?

后端 未结 3 1861
夕颜
夕颜 2020-11-30 10:37

I can pass a StringIO object to pd.to_csv() just fine:

io = StringIO.StringIO()
pd.DataFrame().to_csv(io)

But when using the excel writer,

3条回答
  •  悲哀的现实
    2020-11-30 10:49

    Glancing at the pandas.io.excel source looks like it shouldn't be too much of a problem if you don't mind using xlwt as your writer. The other engines may not be all that difficult either but xlwt jumps out as easy since its save method takes a stream or a filepath.

    You need to initially pass in a filename just to make pandas happy as it checks the filename extension against the engine to make sure it's a supported format. But in the case of the xlwt engine, it just stuffs the filename into the object's path attribute and then uses it in the save method. If you change the path attribute to your stream, it'll happily save to that stream when you call the save method.

    Here's an example:

    import pandas as pd
    import StringIO
    import base64
    
    df = pd.DataFrame.from_csv('http://moz.com/top500/domains/csv')
    xlwt_writer = pd.io.excel.get_writer('xlwt')
    my_writer = xlwt_writer('whatever.xls')  #make pandas happy 
    xl_out = StringIO.StringIO()
    my_writer.path = xl_out  
    df.to_excel(my_writer)
    my_writer.save()
    print base64.b64encode(xl_out.getvalue())
    

    That's the quick, easy and slightly dirty way to do it. BTW... a cleaner way to do it is to subclass ExcelWriter (or one of it's existing subclasses, e.g. _XlwtWriter) -- but honestly there's so little involved in updating the path attribute, I voted to show you the easy way rather than go the slightly longer route.

提交回复
热议问题