【问题标题】:How to decompress a file using python 3.x and gzip如何使用 python 3.x 和 gzip 解压缩文件
【发布时间】:2016-12-21 19:04:29
【问题描述】:

我曾帮助我创建一段代码来压缩来自用户的输入。然后该代码将此压缩版本传输到一个文件中。该代码还将输入发送到文件中,以查看文件已压缩多少。

import gzip, time

plaintext = input("Please enter the text you want to compress: ")
file_ = open('text.txt', 'w')
file_.write(str(plaintext))
file_.close()
with gzip.open('compressed_file.text' + ".gz", "wb") as outfile:
    outfile.write(bytes(plaintext, 'UTF-8'))


with open("data.txt","wb") as fh:
    with open('compressed_file.text.gz', 'rb') as fd:
        fh.write(fd.read())

我想要一些关于如何解压缩文件以进行原始用户输入的帮助。

【问题讨论】:

  • 什么给你带来麻烦?考虑到gzip 模块中的实用程序,这似乎是一个非常简单的问题...
  • 我想解压缩我压缩过的东西。但我不知道如何并且想要一些帮助。第二段代码仅将压缩后的句子发送回用户,它不会生成我想要实现的原始句子。

标签: python gzip zlib compression


【解决方案1】:

我认为,仅读取 GZIP 文件并写回文件会对您有所帮助。

import gzip

plaintext = input("Please enter the text you want to compress: ")

with open("text.txt","wb") as file_:
    file_.write(str(plaintext.encode('utf-8')))

filename = input("Please enter the desired filename: ")
print("Name of file to be zipped is text.txt")
print("Name of GZIP file is ",filename+'.gz')
print("Zipping file...")
with gzip.GzipFile(filename + ".gz", "wb") as outfile:
    outfile.write(open("text.txt").read())

print("Name of unzipped file is unzip_text.txt")
print("Unzipping ...")
with gzip.GzipFile(filename + ".gz", 'rb') as inF:
    with file("unzip_text.txt", 'wb') as outF:
        s = inF.read()
        outF.write(s.encode('utf-8'))

print("Content of unzip file is...")
with open("unzip_text.txt") as fd:
    for line in fd.readlines():
        print line

输出:

C:\Users\dinesh_pundkar\Desktop>python gz.py
Please enter the text you want to compress: "My name is Dinesh"
Please enter the desired filename: "dinesh"
Name of file to be zipped is text.txt
('Name of GZIP file is ', 'dinesh.gz')
Zipping file...
Name of unzipped file is unzip_text.txt
Unzipping ...
Content of unzip file is...
My name is Dinesh

C:\Users\dinesh_pundkar\Desktop>

【讨论】:

  • Traceback(最近一次调用最后一次):文件“E:\GCSE WORK\Computing\A 4.5.3\Coursework\Task 3\Task 3.py”,第 13 行,在 中gzip.open('text.txt.gz', 'rb') as fd: File "C:\Python33\lib\gzip.py", line 52, in open binary_file = GzipFile(filename, gz_mode, compresslevel) File " C:\Python33\lib\gzip.py",第 184 行,在 init 中 fileobj = self.myfileobj = builtins.open(filename, mode or 'rb') FileNotFoundError: [Errno 2] No such文件或目录:'text.txt.gz' >>>
  • 将“text.txt.gz”替换为您在上面的代码创建的 GZIP 文件名
  • 我有,但结果显示“FileNotFoundError: [Errno 2] No such file or directory: 'comp2'” comp2 是文件名。
  • 我修改了代码,它没有给我解压缩文件,它给了我压缩文件
  • 你的代码只是简单地返回压缩文件,它不会解压缩它来做原始输入。
【解决方案2】:

这是I found it on the answer to another question的答案。

plaintext = input('Please enter some text')
filename = 'foo.gz'
with gzip.open(filename, 'wb') as outfile:
    outfile.write(bytes(plaintext, 'UTF-8'))
with gzip.open(filename, 'r') as infile:
    outfile_content = infile.read().decode('UTF-8')
print(outfile_content)

这会压缩输入,将其存储在文件中,并解压缩文件以生成原始输入。然后将此解压缩的输入打印到外壳。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-02-01
    • 1970-01-01
    • 2010-09-06
    • 2012-09-15
    • 1970-01-01
    • 1970-01-01
    • 2018-07-06
    • 2013-02-26
    相关资源
    最近更新 更多