如何快速从大文件中搜索列表内容?

排场

我有一个 CSV 文件,大小为 3 GB。

我想从该文件中快速搜索列表的内容。

有人建议将 CSV 转换为 BLF 文件并应用布隆过滤器。

我是初学者,我对此一无所知。

如果任何人都可以提供简短的工作代码或指向具有相同详细解​​释的页面的链接,那将非常有帮助。

提前致谢 :)

科拉连

您可以将文件转换为数据库 (SQLite):

import csv, sqlite3

# Change column names
fields = ('code1', 'code2', 'firstname', 'lastname', 'genre', 'city', 'country')

# Create the database and the unique table
con = sqlite3.connect("data.db")
cur = con.cursor()
cur.execute(f"CREATE TABLE tbl {fields};")

# Read the csv file and insert rows to database
reader = csv.reader(open('data.csv'))
cur.executemany(f"INSERT INTO tbl {fields} VALUES (?, ?, ?, ?, ?, ?, ?);", reader)

# Create some indexes to increase speed of queries
cur.execute("CREATE INDEX idx_fullname ON tbl (firstname, lastname);")
cur.execute("CREATE INDEX idx_location ON tbl (city, country);")

# Commit and close (Mandatory!)
con.commit()
con.close()

之后,您有一些选择来查询数据库:

  • 浏览器
  • Python/sqlite(与上面类似,但带有 SELECT 语句)
  • 熊猫(pd.read_sql

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章