【问题标题】:convert python sqlite db to hdf5将 python sqlite db 转换为 hdf5
【发布时间】:2014-05-21 00:44:21
【问题描述】:

Pandas DataFrame 可以像这样转换为 hdf5 文件;

df.to_hdf('test_store.hdf','test',mode='w')

我有一个 sqlite db 文件,它必须转换为 hdf5 文件,然后我会使用 pd.read_hdf 通过 pandas 读取 hdf5 文件。

但首先如何将 python sqlite db 转换为 hdf5 文件?

编辑:

我知道在 pandas 中使用 .read_sql 方法。但我想先将 db 转换为 hdf5。

【问题讨论】:

    标签: python sqlite hdf5


    【解决方案1】:

    看看这个---

    http://www.tutorialspoint.com/sqlite/sqlite_limit_clause.htm

    我们的想法是迭代select * from table 查询并通过增加偏移量来限制结果。如上所示,将结果写入 hdf5 数据存储。首先计算带有select count(*) from table 的条目数,然后将迭代拆分为可处理的块。例如,如果有 400 万条记录一次读取 200,000 条,并从 0、200000、400000 等增加偏移量......

    我需要对一个非常大的 sqlite 文件执行此操作。如果有效,将报告。

    【讨论】:

      【解决方案2】:

      这非常简单:使用 pandas!

      pandas 支持将reading data directly from a SQL database 放入 DataFrame。获得 DataFrame 后,您可以随心所欲地使用它。

      简短的例子,取自from the docs:

      import sqlite3
      from pandas.io import sql
      # Create your connection.
      cnx = sqlite3.connect('mydbfile.sqlite')
      
      # read the result of the SQL query into a DataFrame
      data = sql.read_sql("SELECT * FROM data;", cnx)
      
      # now you can write it into a HDF5 file
      data.to_hdf('test_store.hdf','test',mode='w')
      

      【讨论】:

      • 我知道 `.read_sql' 方法。但我在这里处理大小问题,因此想先将 db 转换为 hdf5。
      • @richie 我添加了转换为 HDF5 的最后一步。鉴于您的问题,我认为这很明显。
      猜你喜欢
      • 1970-01-01
      • 2016-10-20
      • 2015-01-27
      • 1970-01-01
      • 2017-01-25
      • 1970-01-01
      • 2018-07-29
      • 1970-01-01
      • 2016-12-13
      相关资源
      最近更新 更多