想通过 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!
可是官方示例和我写的没什么区别啊,懵逼了....
改成: