在给出位置后压缩用户的句子

Compress user's sentence after positions are given

我在下面完成了这段代码,并在压缩部分获得了一些帮助。代码不工作,我不知道如何修复它。在我的 class 中,我们仍然是初学者,所以我不太好。另外,当它要求用户输入一个句子时,为什么它是 b'_' 而不是 Please enter a sentence?

import gzip
File = open('Users Sentence.txt', 'w') 
Sentence = input(b"Please input a sentence ")                                                                
print (Sentence)                                                                                            
varNameIn = Sentence.encode('utf8')
Lower = Sentence.lower()                                                                                    
Splitsentence = Lower.split()                                                                                                                                                              

Userfile = File.write(Sentence)                                                                             
varNameIn = Splitsentence 
varNameOut = gzip.compress(varNameIn)
print(varNameOut)

varNameDecon = gzip.decompress(varNameOut)
print(varNameDecon.decode('utf-8'))

positions = {Splitsentence:index for index, Splitsentence in reversed(list(enumerate(Splitsentence, 1)))}   
fileposition = (' '.join(str(positions.get(word)) for word in Splitsentence))
print (fileposition)                                                                                        
Userfile = File.write(fileposition)                                                                         
File.close()                                                                                                

它给了我这个错误信息,但我不明白:

Traceback (most recent call last):
  File "\USER-PC\Users\user\Documents\homework\CW Computing.py", line 11, in <module>
    varNameOut = gzip.compress(varNameIn)
  File "C:\Python34\lib\gzip.py", line 624, in compress
    f.write(data)
  File "C:\Python34\lib\gzip.py", line 343, in write
    self.crc = zlib.crc32(data, self.crc) & 0xffffffff
TypeError: 'list' does not support the buffer interface

谢谢。

你的代码加上我的注释:

Sentence = input(b"Please input a sentence ") # Sentence is type str
print (Sentence)
varNameIn = Sentence.encode('utf8') # not used
Lower = Sentence.lower() # Lower is type str
Splitsentence = Lower.split() Splitsentence is a list of str objects
Userfile = File.write(Sentence) # Userfile = number of chars written (??)
varNameIn = Splitsentence # varNameIn is a list of str objects
varNameOut = gzip.compress(varNameIn) # attempting to compress a list

您需要重新考虑what/why您希望进行数据压缩...