我正在使用库读取.xlxs文件并提取dataframe。现在,我正在尝试创建一个具有.ods扩展名的文件,并使用耗氧物质库将dataframe写入其中。这是我的密码:--
import pandas as pd
from pyexcel_ods import save_data
from collections import OrderedDict
self.current_df = pd.read_excel('filename')
data = OrderedDict()
data.update(df)
save_data("as.ods", data)这是抛错
{TypeError}'int‘对象不可迭代
可以自由地要求更多代码。
注意:--我使用的是Python 3。
发布于 2019-11-19 07:55:34
我认为您的解决方案的问题是,来自save_data的py_ods函数没有以它所需的格式获取数据。
解释在评论中。
# coding: utf-8
import pandas as pd
from pyexcel_ods import save_data
from collections import OrderedDict
def save_ods_from_excel(excel_file, target_ods_file):
# First read into dataframe
df = pd.read_excel(excel_file)
# Change everything to string since we're just writing
df = df.astype(str)
# Initiliaze data to be written as an empty list, as pyods needs a list to write
whole_data_list = []
# Initiliaze the empty dict() for the data
d = OrderedDict()
# Write the columns first to be the first entries
whole_data_list.append(list(df.columns))
# loop through data frame and update the data list
for index, row in df.iterrows():
whole_data_list.append(list(row.values))
# Populate dict() with updated data list
d.update({"Moved sheet": whole_data_list})
# Finally call save_data() from pyods to store the ods file
save_data(target_ods_file, d)在这些数据来自微软。中尝试了上面的方法
>>> save_ods_from_excel('/Users/gkm/Downloads/Financial Sample.xlsx', '/tmp/sample-financial.ods')更多信息pyexcel ods文档
发布于 2020-08-02 22:25:40
请注意,在最近版本的Pandas (当前为1.1)已经实现了对ODS格式的功能,如pd.ExcelWriter()和excel()的支持。您只需要指定propper引擎"odf“才能处理OpenDocument文件格式(.odf、.ods、.odt)。
您需要安装odfpy包。
发布于 2019-11-19 06:36:10
试试这个:
from collections import OrderedDict
from pyexcel_ods import get_data
current_df = pd.read_excel('array.xlsx')
data = OrderedDict()
data.update({ "Sheet_1": current_df.to_numpy().tolist() })
save_data("as.ods", data)
data = get_data("as.ods")
data
#OrderedDict([('Sheet_1',
# [['b', 121], ['c', 98], ['d', 9], ['e', 100], ['f', 45]])])https://stackoverflow.com/questions/58915626
复制相似问题