如何从ODS电子表格导入数据(由OpenOffice、LibreOffice等使用)在朱莉娅DataFrame车里?
(这是一个社区维基问答)
发布于 2016-12-06 23:42:46
如果机器上安装了python,则Julia可以通过PyCall非常直接地使用ezodf模块
using PyCall
using DataFrames
@pyimport ezodf
doc = ezodf.opendoc("test.ods")
nsheets = length(doc[:sheets])
println("Spreadsheet contains $nsheets sheet(s).")
for sheet in doc[:sheets]
println("---------")
println(" Sheet name : $(sheet[:name])")
println("Size of Sheet : (rows=$(sheet[:nrows]()), cols=$(sheet[:ncols]()))")
end
# convert the first sheet to a dictionary
sheet = doc[:sheets][1]
df_dict = Dict()
col_index = Dict()
for (i, row) in enumerate(sheet[:rows]())
# row is a list of cells
# assume the header is on the first row
if i == 1
# columns as lists in a dictionary
[df_dict[cell[:value]] = [] for cell in row]
# create index for the column headers
[col_index[j]=cell[:value] for (j, cell) in enumerate(row)]
continue
end
for (j, cell) in enumerate(row)
# use header instead of column index
append!(df_dict[col_index[j]],cell[:value])
end
end
# and convert the dictionary to a DataFrame
df = DataFrame(df_dict)(这只是用Julia重写了davidovitch的python代码on this answer)
编辑:
现在,我根据这段代码编写了一个Julia包:OdsIO。
它提供了几个函数来从ods文件导入数据(包括单元格范围),希望很快也能支持导出。
EDIT2:
从v0.1.0开始支持导出到Ods
https://stackoverflow.com/questions/40999285
复制相似问题