将Dataframe写入ODS格式
Write Dataframe into ODS format
我正在使用 pandas library and extracting the dataframe
. Now I am trying to create a file with .ods extension and write the dataframe
into it using pyexcel_ods 库读取 .xlxs 文件。
这是我的代码:--
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' object is not iterable
欢迎索取更多代码。
注意 :-- 我正在使用 Python 3.
试试这个:
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]])])
Soham 试试这个,我认为你的解决方案的问题是 py_ods 中的 save_data
函数没有以它需要的格式获取数据。
解释在评论里
# 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')
请注意,在 Pandas 的最新版本(当前为 1.1)中,已在 pd.ExcelWriter() and pd.read_excel() 等函数中实现了对 ODS 格式的支持。您只需指定适当的引擎“odf”即可使用 OpenDocument 文件格式(.odf、.ods、.odt)
我正在使用 pandas library and extracting the dataframe
. Now I am trying to create a file with .ods extension and write the dataframe
into it using pyexcel_ods 库读取 .xlxs 文件。
这是我的代码:--
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' object is not iterable
欢迎索取更多代码。
注意 :-- 我正在使用 Python 3.
试试这个:
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]])])
Soham 试试这个,我认为你的解决方案的问题是 py_ods 中的 save_data
函数没有以它需要的格式获取数据。
解释在评论里
# 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')
请注意,在 Pandas 的最新版本(当前为 1.1)中,已在 pd.ExcelWriter() and pd.read_excel() 等函数中实现了对 ODS 格式的支持。您只需指定适当的引擎“odf”即可使用 OpenDocument 文件格式(.odf、.ods、.odt)