Soh*_*ham 2 python python-3.x pandas pyexcel
我正在使用pandas库读取.xlxs文件并提取.xlxs文件。现在我正在尝试创建一个扩展名为.ods的文件,并使用pyexcel_ods库将其写入其中。这是我的代码:--dataframedataframe
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)
Run Code Online (Sandbox Code Playgroud)
它正在抛出错误
{TypeError}'int' 对象不可迭代
随意索取更多代码。
注意:--我使用的是 Python 3。
请注意,在 Pandas 的最新版本(当前为 1.1)中,已在pd.ExcelWriter()和pd.read_excel()等函数中实现了对 ODS 格式的支持。您只需要指定 propper 引擎“odf”即可处理 OpenDocument 文件格式(.odf、.ods、.odt)
Soham 尝试这个,我认为您的解决方案的问题是save_datapy_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)
Run Code Online (Sandbox Code Playgroud)
使用来自微软的数据尝试了上述方法
>>> save_ods_from_excel('/Users/gkm/Downloads/Financial Sample.xlsx', '/tmp/sample-financial.ods')
Run Code Online (Sandbox Code Playgroud)
更多信息请参阅 pyexcel ods 文档
| 归档时间: |
|
| 查看次数: |
3903 次 |
| 最近记录: |