简体   繁体   English

从BytesIO解压缩bz2文件

[英]decompress a bz2 file from BytesIO

I want to read a bz2 file in a server, decompress it and read with csv parser, but I still have the error; 我想读取服务器中的bz2文件,将其解压缩并使用csv解析器读取,但是仍然出现错误;

    myfile = bz2.BZ2File(bio.read(), "rb")
TypeError: file() argument 1 must be encoded string without NULL bytes, not str

import paramiko
from config import config
import bz2
import csv
import StringIO
from io import BytesIO
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(config.get('mrc_ssh', 'host'), username=config.get('mrc_ssh', 'user'))
sftp_client = ssh.open_sftp()
_file = sftp_client.open('/home/myfile.bz2')
bio = BytesIO(_file.read())
print bio
myfile = bz2.BZ2File(bio.read(), "rb")
reader = csv.DictReader(myfile)
for row in reader:
    print row

bz2.BZ2File takes a filename as the first argument. bz2.BZ2File文件名作为第一个参数。 Not actual data. 不是实际数据。

Either use (if you can store locally the file): 两种使用方式(如果可以在本地存储文件):

myfile = bz2.BZ2File('/home/myfile.bz2', "rb")

Or use the one-shot decompression function bz2.decompress 或使用一次解压缩功能bz2.decompress

uncompressed_data = bz2.decompress(bio.read())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM