查看: 1932|回复: 6

TypeError: 'str' does not support the buffer interface

[复制链接]

5

主题

41

帖子

108

积分

注册会员

Rank: 2

积分
108
发表于 2018-9-19 16:30:46 | 显示全部楼层 |阅读模式
  1. plaintext = input("Please enter the text you want to compress")
  2. filename = input("Please enter the desired filename")
  3. with gzip.open(filename + ".gz", "wb") as outfile:
  4.     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
回复

使用道具 举报

11

主题

63

帖子

159

积分

注册会员

Rank: 2

积分
159
发表于 2018-9-19 16:33:43 | 显示全部楼层
如果使用Python3x,那么string与Python 2.x的类型不同,则必须将其转换为字节(对其进行编码)。
  1. plaintext = input("Please enter the text you want to compress")
  2. filename = input("Please enter the desired filename")
  3. with gzip.open(filename + ".gz", "wb") as outfile:
  4.     outfile.write(bytes(plaintext, 'UTF-8'))
复制代码

也不要使用变量名,例如,那些是模块或函数的名称比如string或者file。
非ASCII文本也被压缩/解压缩。我使用UTF-8编码的波兰语字母:
  1. plaintext = 'Polish text: ąćęłńóśźżĄĆĘŁŃÓŚŹŻ'
  2. filename = 'foo.gz'
  3. with gzip.open(filename, 'wb') as outfile:
  4.     outfile.write(bytes(plaintext, 'UTF-8'))
  5. with gzip.open(filename, 'r') as infile:
  6.     outfile_content = infile.read().decode('UTF-8')
  7. print(outfile_content)
复制代码

回复

使用道具 举报

4

主题

37

帖子

98

积分

注册会员

Rank: 2

积分
98
发表于 2018-9-19 16:38:16 | 显示全部楼层
这个问题有一个更简单的解决方案。
您只需要在模式中添加一个wt即可。这会导致Python将文件作为文本文件而不是二进制文件打开。
完整的程序变成了这样:
  1. plaintext = input("Please enter the text you want to compress")
  2. filename = input("Please enter the desired filename")
  3. with gzip.open(filename + ".gz", "wt") as outfile:
  4.     outfile.write(plaintext)
复制代码

回复

使用道具 举报

9

主题

74

帖子

185

积分

注册会员

Rank: 2

积分
185
发表于 2018-9-19 16:39:31 | 显示全部楼层
对于Python 3.x,你可以通过以下方式将文本转换为原始字节:
bytes("my data", "encoding")
例如:
bytes("attack at dawn", "utf-8")
将使用outfile.write返回对象

回复

使用道具 举报

4

主题

33

帖子

88

积分

注册会员

Rank: 2

积分
88
发表于 2018-9-19 16:41:31 | 显示全部楼层
从py2切换到py3时,通常会发生此问题。在py2 plaintext中,字符串和字节数组都是类型。在py3 plaintext中只是一个字符串,并且outfile.write()方法在二进制模式下打开outfile时实际上采用了一个字节数组,因此引发了异常。更改输入以plaintext.encode('utf-8')解决问题。
在py2中,file.write的声明使它看起来像你传入一个字符串:file.write(str)。实际上你传入一个字节数组, file.write(bytes)。file.write(bytes)需要一个字节类型,并在py3中从你转换它的str中获取字节:
  1. py3>> outfile.write(plaintext.encode('utf-8'))
复制代码
回复

使用道具 举报

10

主题

82

帖子

200

积分

中级会员

Rank: 3Rank: 3

积分
200
发表于 2018-9-19 16:42:44 | 显示全部楼层
你无法将Python 3'字符串'序列化为字节,而无需将explict转换为某些编码。
  1. outfile.write(plaintext.encode('utf-8'))
复制代码

可能是你想要的。这也适用于python 2.x和3.x.
回复

使用道具 举报

0

主题

32

帖子

80

积分

注册会员

Rank: 2

积分
80
发表于 2018-10-10 08:13:47 | 显示全部楼层
气氛真好啊,大家都很热心
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表