2017-01-18 8 views
0

私はこのコードを以下の通り実行し、圧縮部分について助けをしました。コードが動作していないと私はそれを修正する方法を理解していない。私のクラスではまだ初心者なので、あまりにも良くない。また、ユーザーに文章を求めるときに、なぜ文を入力してください。位置が与えられた後にユーザーの文章を圧縮する

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\2.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 

感謝を。私の注釈付き

+0

ワイルド推測:あなたは、リストを圧縮することはできません。文字列など、何か他のものを圧縮してみてください。 – Kevin

+0

ドキュメントは少し不明ですが、['bytes'](https://docs.python.org/3/library/functions.html#bytes)のようなオブジェクトしか圧縮できないと思います。 –

+0

'pickle'を' gzip'で圧縮して 'list'を圧縮します –

答えて

1

あなたのコード:

あなたはデータ圧縮を行うことを希望する理由を何/再考する必要がある
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 

...

関連する問題