写入文本到gzip文件
- 论坛
- 写入文本到gzip文件
14 浏览
写入文本到gzip文件
根据在博客和其他帖子中找到的教程和示例,似乎写入.gz
文件的方法是以二进制模式打开它并将字符串原封不动地写入:
import gzip with gzip.open('file.gz', 'wb') as f: f.write('Hello world!')
我尝试了一下,但出现了以下异常:
File "C:\Users\Tal\Anaconda3\lib\gzip.py", line 258, in write data = memoryview(data) TypeError: memoryview: a bytes-like object is required, not 'str'
所以我尝试以文本模式打开文件:
import gzip with gzip.open('file.gz', 'w') as f: f.write('Hello world!')
但我得到了相同的错误:
File "C:\Users\Tal\Anaconda3\lib\gzip.py", line 258, in write data = memoryview(data) TypeError: memoryview: a bytes-like object is required, not 'str'
如何解决Python3中的这个问题?