首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何读取ods文档(例如,LibreOffice计算)并将其转换为Julia数据帧?

如何读取ods文档(例如,LibreOffice计算)并将其转换为Julia数据帧?
EN

Stack Overflow用户
提问于 2016-12-06 23:42:46
回答 1查看 1.2K关注 0票数 0

如何从ODS电子表格导入数据(由OpenOffice、LibreOffice等使用)在朱莉娅DataFrame车里?

(这是一个社区维基问答)

EN

回答 1

Stack Overflow用户

发布于 2016-12-06 23:42:46

如果机器上安装了python,则Julia可以通过PyCall非常直接地使用ezodf模块

代码语言:javascript
复制
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

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40999285

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档