如果使用Python3x,则
string与Python 2.x的类型不同,则必须将其转换为字节(对其进行编码)。
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(bytes(plaintext, 'UTF-8'))
也不要使用像string或那样的变量file名作为模块或函数的名称。
是的,非ASCII文本也会被压缩/解压缩。我使用UTF-8编码的波兰字母:
plaintext = 'Polish 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().depre('UTF-8')print(outfile_content)
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)