Python GZip和输出文件

Python GZip和输出文件,python,json,gzip,Python,Json,Gzip,我对下面的代码有困难(这是我用Python开发的一个更大的应用程序简化的代码) 运行此操作时,我收到以下错误: File "d:\Development\AWS\TwitterCompetitionsStreaming.py", line 61, in on_status with gzip.GzipFile(fileobj=out, mode="w") as f: File "C:\Python38\lib\gzip.py"

我对下面的代码有困难(这是我用Python开发的一个更大的应用程序简化的代码)

运行此操作时,我收到以下错误:

File "d:\Development\AWS\TwitterCompetitionsStreaming.py", line 61, in on_status
    with gzip.GzipFile(fileobj=out, mode="w") as f:
  File "C:\Python38\lib\gzip.py", line 204, in __init__
    self._write_gzip_header(compresslevel)
  File "C:\Python38\lib\gzip.py", line 232, in _write_gzip_header
    self.fileobj.write(b'\037\213')             # magic header
TypeError: string argument expected, got 'bytes'
PS忽略这里的垃圾缩进…我知道它看起来不对劲


我想做的是创建一个json文件,并在将gzip文件保存到文件系统(windows)之前将其gzip到内存中。我知道我走错了方向,可能需要一个指针。非常感谢。

使用gzip时,您必须在任何地方使用字节,而不是字符串和文本。首先,使用BytesIO而不是StringIO。第二,对于字节,模式应该是
'wb'
,而不是
'w'
(最后一个是文本)(在追加时,同样
'ab'
而不是
'a'
),这里
'b'
字符表示“字节”。完整更正代码如下:

几乎是重复的(
ZipFile
GzipFile
)——但答案是一样的。清楚地理解Python 3中str和bytes之间的区别,然后使用正确的
File "d:\Development\AWS\TwitterCompetitionsStreaming.py", line 61, in on_status
    with gzip.GzipFile(fileobj=out, mode="w") as f:
  File "C:\Python38\lib\gzip.py", line 204, in __init__
    self._write_gzip_header(compresslevel)
  File "C:\Python38\lib\gzip.py", line 232, in _write_gzip_header
    self.fileobj.write(b'\037\213')             # magic header
TypeError: string argument expected, got 'bytes'
from io import BytesIO
import gzip

jsonString = 'JSON encoded string here created by a previous process in the application'

out = BytesIO()
with gzip.GzipFile(fileobj = out, mode = 'wb') as f:
    f.write(str.encode(jsonString))
    
currenttimestamp = '2021-01-29'

# Write the file once finished rather than streaming it - uncomment the next line to see file locally.
with open("out_" + currenttimestamp + ".json.gz", "wb") as f:
    f.write(out.getvalue())