こんにちは私はロードするためのコマンドのコピーを使用行のpostgresからのバイナリ文字列を圧縮取得する方法
def test_insert():
str_test = '4 1 2\n 2 4 5\n'.encode('utf8')
cmpstr = zlib.compress(str_test)
str_test_to_write = '\\x' + cmpstr.encode('hex_codec')
with open('outfile.txt','w') as output_file:
output_file.write(str(1) + '|'+ str_test_to_write + '\n')
output_file.write(str(2) + '|'+ str_test_to_write + '\n')
そして、Postgresデータベースに挿入される圧縮文字列をテキストファイルを生成し、このコードを持っています私のテーブルへの情報:
time cat outfile.txt |psql teste3 -c "\copy zstr(id,zstr) from stdout with delimiter '|'"
これは私のテーブルです:
drop table if exists zstr;
create table zstr(
id int,
zstr bytea,
primary key(id));
その後、私はしたいです私の文字列を選択するが、私はこのエラーが表示されます:
>>> import psycopg2
>>> import zlib
>>> con = psycopg2.connect(host = 'X', database = 'Y', user = 'Z')
>>> con.autocommit = True
>>> cur = con.cursor()
>>> cur.execute('select * from zstr where id = 1')
>>> row = cur.fetchone()
>>> row
(1, <read-only buffer for 0x7fe19b75f270, size 41, offset 0 at 0x7fe196976f30>)
>>> a = str(row[1])
>>> q = zlib.decompress(a)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
zlib.error: Error -3 while decompressing data: incorrect header check
どのように私の文字列を取得できますか?
私が欲しいの出力:
'4 1 2\n 2 4 5\n'
なぜzlibは文字列を圧縮していますか? –