pd.ExcelWriter 错误:Append mode is not supported with xlsxwriter!

发布于 2022-09-11 17:54:53 字数 1528 浏览 9 评论 0

想通过 Pandas 给一张 excel 追加几条数据,查了文档发现 Pandas.ExcelWriter (http://pandas.pydata.org/pand...)有这个函数,不过如果需要向excel追加数据,使用时要将参数mode设置为"a",于是写了下面这段程序:

import pandas as pd                                                     

df = pd.DataFrame(data={'a':[4], 'b':['玉米'], 'c':[0.5]})              

with pd.ExcelWriter("test.xlsx", mode='a') as writer: 
            df.to_excel(writer) 
            

其中 test.xlsx 是一个包含 a, b, c 字段名的xlsx表格。
运行程序出现错误:

ValueError                                Traceback (most recent call last)
<ipython-input-3-c643d22b4217> in <module>
----> 1 with pd.ExcelWriter("test.xlsx", mode='a') as writer:
      2     df.to_excel(writer)
      3 

~/anaconda/lib/python3.6/site-packages/pandas/io/excel.py in __init__(self, path, engine, date_format, datetime_format, mode, **engine_kwargs)
   1935 
   1936         if mode == 'a':
-> 1937             raise ValueError('Append mode is not supported with xlsxwriter!')
   1938 
   1939         super(_XlsxWriter, self).__init__(path, engine=engine,

ValueError: Append mode is not supported with xlsxwriter!

可是官方示例和我写的没什么区别啊,懵逼了....

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笑红尘 2022-09-18 17:54:53

改成:

with pd.ExcelWriter("test.xlsx", mode='a', engine="openpyxl") as writer: 
     df.to_excel(writer) 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文