我正在嘗試調整 Python 2.7 例程來為 TSV 檔案的電子閱讀器創建 SQLite db 字典。
所示的例程在第 71-74 行(# compress and save)中創建了一個 html 字串的 (pk)zip 檔案。我希望壓縮檔案改為 gzip 格式。我對 Python 非常非常不滿意,我一直在尋找使用 gzip 壓縮的示例,試圖將它們融入這個例程(我已經以其他方式適應了我自己的原始使用方式)。我知道也需要在第 9 行(匯入)中進行更改,但不確定是哪個。
#!/usr/bin/python
# -*- coding: utf-8 -*-
# by Jiri Orsag, 2014
# https://github.com/geoRG77/nook-dictionary
# Many thanks to Homeless Ghost for his script 'createRenateNSTdictionaryfromnookdictionarydb.py'
# which was a great source of ideas for my work
import sqlite3, sys, zipfile, zlib, os
# config
DICTIONARY_FILE = 'test.txt' # input file (needed)
OUTPUT_DB = 'ox_en_GB.db' # output file
TEMP_DIRECTORY = './temp/' # will be deleted after successful run
STEP = 10000 # for print message
########################################################
print 'Converting dictionary...'
con = sqlite3.connect(OUTPUT_DB)
con.text_factory = str
cur = con.cursor()
index = 0
duplicateCount = 1
prevTerm = ''
try:
if not os.path.exists(TEMP_DIRECTORY):
os.makedirs(TEMP_DIRECTORY)
# open dict file
dict = open(DICTIONARY_FILE, 'r')
# delete previous tables
cur.execute('DROP TABLE IF EXISTS android_metadata')
cur.execute('DROP TABLE IF EXISTS tblWords')
# create tables
cur.execute('CREATE TABLE "android_metadata"("locale" TEXT)')
cur.execute('CREATE TABLE "tblWords"("_id" INTEGER PRIMARY KEY AUTOINCREMENT, "term" TEXT COLLATE nocase, "description" BLOB)')
# convert dict to sql
for line in dict:
index = 1
# uncomment next line to debug
# print '# current line = %d' % index
# split line
data = line.split('\t')
term = data.pop(0)
# create HTML
html = '<b>' term '</b>' data[0].strip()
# check for duplicates
if term == prevTerm:
duplicateCount = 1
termEdited = term '[' str(duplicateCount) ']'
else:
termEdited = term
duplicateCount = 1
# create html file
term_stripped = termEdited.replace('/', '')
temp_html = open(TEMP_DIRECTORY term_stripped, 'wb')
temp_html.write(html)
temp_html.close()
# compress & save
zf = zipfile.ZipFile('_temp', mode='w')
zf.write(TEMP_DIRECTORY term_stripped)
zf.close()
# read & insert compressed data
temp_compressed = open('_temp', 'rb')
compressed = temp_compressed.read()
cur.execute('INSERT INTO tblWords (_id, term, description) VALUES(?, ?, ?)', (index, termEdited, sqlite3.Binary(compressed)))
# if duplicate then update previous row with [1]
if duplicateCount == 2:
cur.execute('UPDATE tblWords SET term="' str(term "[1]") '" WHERE _id=' str(index - 1) '')
os.remove(TEMP_DIRECTORY term_stripped)
prevTerm = term
# print _id, term, description
if ((index % STEP) == 0):
print '# current line = %d' % index
#if index == 100:
# break;
# create term_index
cur.execute('CREATE INDEX term_index on tblWords (term ASC)')
cur.execute('SELECT * FROM tblWords order by _id LIMIT 10')
dict.close
# os.remove('_temp')
# os.rmdir(TEMP_DIRECTORY)
except Exception, e:
raise
else:
pass
finally:
pass
print 'Done. ' str(index) ' lines converted.'
有什么提示或好的參考嗎?
uj5u.com熱心網友回復:
import gzip并替換zipfile.ZipFile為gzip.open. 將您寫入臨時檔案的內容寫入 gzip 檔案,這不再需要。例如
gz = gzip.open('_temp', mode='wb')
gz.write(html)
gz.close()
并擺脫temp_html線條和os.remove()它的。我推薦bin 模式,由于某種原因,該 zip 檔案缺少該模式。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/493001.html
標籤:python-2.7 压缩包
上一篇:timm:typeError:'builtin_function_or_method'物件不可下標
下一篇:熱圖中的重疊標簽
