栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

使用Pandas Excelwriter写到StringIO对象?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用Pandas Excelwriter写到StringIO对象?

尽管每个编写器引擎都支持,但Pandas期望ExcelWriter构造函数的文件名路径

StringIO
。也许应该将其作为熊猫中的错误/功能请求提出。

同时,这是使用Pandas

xlsxwriter
引擎的解决方法示例:

import pandas as pdimport StringIOio = StringIO.StringIO()# Use a temp filename to keep pandas happy.writer = pd.ExcelWriter('temp.xlsx', engine='xlsxwriter')# Set the filename/file handle in the xlsxwriter.workbook object.writer.book.filename = io# Write the data frame to the StringIO object.pd.Dataframe().to_excel(writer, sheet_name='Sheet1')writer.save()xlsx_data = io.getvalue()

更新 :从Pandas 0.17开始,现在可以更直接地执行此操作:

# Note, Python 2 example. For Python 3 use: output = io.BytesIO().output = StringIO.StringIO()# Use the StringIO object as the filehandle.writer = pd.ExcelWriter(output, engine='xlsxwriter')

另请参阅XlsxWriter文档中的将数据框输出保存到字符串。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/617696.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号