1
当我想这句话压缩到ASCII对等,我不断收到此错误代码TypeError: ord() expected a character, but string of length 5 found
压缩句子转化成ASCII码,然后解压回
如何解决下面的代码
def menu():
print("Compress a file Press 1")
print("Decompress a file Press 2")
print("Quit Press 3")
user_choice = input("")
if user_choice=="1":
compressing()
elif user_choice=="2":
decompressing()
elif user_choice=="3":
import sys
sys.exit()
else:
print("Input invalid.Please enter a number for path selection") , "\n" , menu()
def compressing():
compressed_sentence=[]
sentence=input("Enter a sentence: ")
sentence=sentence.split()
for i in range(len(sentence)):
character=(sentence[i])
ascii_character=ord(character)
compressed_sentence.append(ascii_character)
with open('compressed_file.txt','w') as myFile:
myFile.write(str(compressed_sentence))
menu()
def decompressing():
with open('compressed_file.txt','r') as myFile:
sentence=myFile.read()
for i in sentence:
if i in "[],'":
sentence=sentence.replace(i," ")
new_sentence=sentence.split()
decompressed_sentence=str("")
for i in range(len(new_sentence)):
character=int(new_sentence[i])
decompressed_sentence=(decompressed_sentence+(chr(character)))
final_decompressed_sentence=decompressed_sentence.split()
print(final_decompressed_sentence)
with open('decompressed_file.txt','w') as myFile:
myFile.write(final_decompressed_sentence)
menu()
menu()
为了能够正确地压缩和解压缩它。