为了账号安全,请及时绑定邮箱和手机立即绑定

TypeError:“Str”不支持缓冲区接口

TypeError:“Str”不支持缓冲区接口

鸿蒙传说 2019-06-19 19:33:34
TypeError:“Str”不支持缓冲区接口plaintext = input("Please enter the text you want to compress")filename = input("Please enter the desired filename") with gzip.open(filename + ".gz", "wb") as outfile:     outfile.write(plaintext)上面的python代码给出了以下错误:Traceback (most recent call last):   File "C:/Users/Ankur Gupta/Desktop/Python_works/gzip_work1.py", line 33, in <module>     compress_string()   File "C:/Users/Ankur Gupta/Desktop/Python_works/gzip_work1.py", line 15, in compress_string     outfile.write(plaintext)   File "C:\Python32\lib\gzip.py", line 312, in write     self.crc = zlib.crc32(data, self.crc) & 0xffffffff TypeError: 'str' does not support the buffer interface
查看完整描述

3 回答

?
狐的传说

TA贡献1804条经验 获得超3个赞

这个问题有一个更容易解决的办法。

您只需添加一个t到模式,所以它变成wt..这导致Python以文本文件的形式打开文件,而不是二进制文件。一切都会正常的。

完整的程序如下:

plaintext = input("Please enter the text you want to compress")filename = input("Please enter the desired filename")
with gzip.open(filename + ".gz", "wt") as outfile:
    outfile.write(plaintext)


查看完整回答
反对 回复 2019-06-19
?
忽然笑

TA贡献1806条经验 获得超5个赞

如果不明确地将Python 3‘字符串’序列化为字节,则不能将其序列化为某种编码。

outfile.write(plaintext.encode('utf-8'))

可能是你想要的。这也适用于python2.x和3.x。


查看完整回答
反对 回复 2019-06-19
  • 3 回答
  • 0 关注
  • 533 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号