2017-01-18 7 views
0

나는 아래의 코드를 수행했고 압축 부분에 대한 도움을 받았습니다. 코드가 작동하지 않고 그것을 수정하는 방법을 이해할 수 없습니다. 수업 시간에 우리는 아직 초보자이므로 너무 좋지 않습니다. 또한 사용자가 문장을 요구할 때 왜 문장을 입력하지 않고 b'_ '로 표시됩니까?위치가 부여 된 후 사용자의 문장 압축하기

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 

...