10
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

python データーを圧縮してsqliteへ書き込む

Posted at
compress.py
#!/usr/bin/python
# -*- encoding: utf-8 -*-
import sys

import zlib
from base64 import binascii

#-----------------------------------------------------------
# COMPRESS
#-----------------------------------------------------------
def z_compress(data):

    #return buffer(zlib.compress(data))
    return zlib.compress(data)

#-----------------------------------------------------------
# DECOMPRESS
#-----------------------------------------------------------
def z_decompress(data):

    return zlib.decompress(data)

#-----------------------------------------------------------
# B2A
#   Binary >> Base64 Ascii
#-----------------------------------------------------------
def b2a(data):

    return binascii.b2a_base64(data)

#-----------------------------------------------------------
# A2B
#   Base64 Ascii >> Binary
#-----------------------------------------------------------
def a2b(data):

    return binascii.a2b_base64(data)

#-----------------------------------------------------------
# TEST
#-----------------------------------------------------------
def test():

    data = '1234567890123456789012345678901234567890123456789012345678901234567890'

    print 'ROW DATA:   %3d bytes' % sys.getsizeof(data)

    data = z_compress(data)
    print 'COMPRESS:   %3d bytes' % sys.getsizeof(data)

    data = b2a(data)
    print 'B2A:        %3d bytes' % sys.getsizeof(data)

    data_lists = sqlite_test(data)

    for data in data_lists:
        print 'ROW_DATA:   %3d bytes' % sys.getsizeof(data)

        data = a2b(data)
        print 'A2B:        %3d bytes' % sys.getsizeof(data)

        data = z_decompress(data)
        print 'DECOMPRESS: %3d bytes' % sys.getsizeof(data)

def sqlite_test(data):

    import sqlite3

    #--- sqliteに接続
    con = sqlite3.connect(":memory:")
    cur = con.cursor()

    #--- テーブル有無確認
    sql = 'SELECT COUNT(*) FROM sqlite_master WHERE type="table" AND name=?'
    cur.execute(sql, ('test',))

    #---テーブルがない時作成
    if not cur.fetchone()[0]:
        sql = 'CREATE TABLE test(data TEXT)'
        con.execute(sql)

    #--- データー突込む
    sql  = 'INSERT INTO test (data) VALUES (?)'
    con.execute(sql, (data,))
    con.commit()

    #---データー取得
    sql = 'SELECT * from test'
    cur.execute(sql)
    row_lists = []
    for row in cur:
        row_lists.append(row[0])

    #---データーを返す
    return row_lists

if __name__ == '__main__' : test()

結果

ROW DATA:   110 bytes
COMPRESS:    61 bytes
B2A:         69 bytes
ROW_DATA:   168 bytes
A2B:         61 bytes
DECOMPRESS: 110 bytes
10
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?