【问题标题】:importing blocks of data in txt file into an sqlite3 database将txt文件中的数据块导入sqlite3数据库
【发布时间】:2015-04-13 11:14:52
【问题描述】:

我想创建一个使用 python 的 sqlite3 数据库。我正在使用 ArnetMiner 数据集,其中每个实体都有“块”数据。一个block的描述如下:

    #* --- paperTitle
    #@ --- Authors
    #year ---- Year
    #conf --- publication venue
    #citation --- citation number (both -1 and 0 means none)
    #index ---- index id of this paper
    #arnetid ---- pid in arnetminer database
    #% ---- the id of references of this paper (there are multiple lines, with each indicating a reference)
    #! --- Abstract

以下是一个例子:

#*Spatial Data Structures.
#@Hanan Samet,Wei Lee Chang,Jose Fernandez
#year1995
#confModern Database Systems
#citation2743
#index25
#arnetid27
#%165
#%356
#%786754
#%3243
#!An overview is presented of the use of spatial data structures in spatial databases. The focus is on hierarchical data structures, including a number of variants of quadtrees, which sort the data with respect to the space occupied by it. Such techniques are known as spatial indexing methods. Hierarchical data structures are based on the principle of recursive decomposition. 

这是我的问题:

如何将它导入到我创建的 sqlite3 表中?

通常我使用的数据集只是用制表符分隔,所以我会在创建表格后说以下内容:

.separator "\t"
.import Data.txt table_name

我创建的表格如下:

CREATE TABLE publications (
    PaperTitle varchar(150),
    Year int,
    Conference varchar(150),
    Citations int,
    ID int primary key,
    arnetId int,
    Abstract text
);

CREATE TABLE authors (
    ID int primary key,
    Name varchar (100)
);

CREATE TABLE authors_publications (
    PaperID int,
    AuthorID int
);

CREATE TABLE publications_citations (
    PaperID int,
    CitationID int
);

基本上,我想我想问是否有一种快速的方法可以将数据集导入到我创建的数据库表中?还是我必须编写 python 脚本并一次插入每个块?

【问题讨论】:

  • 我不认为 sqlite3 能够去除前缀(即year1995),我认为你最好的选择是编写一个脚本来做到这一点,比如this 是可能是你最好的选择。
  • 写个python导入脚本没有错。

标签: python regex sqlite separator


【解决方案1】:

最好的方法是自己解析数据并将其重写为 csv 文件,然后直接将它们导入我的数据库表。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-11-16
    • 2012-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多