Skip to main content
 首页 » 编程设计

python之我需要为我的 GCSE 计算机科学制作一个简单的文件压缩系统

2025年02月15日8langtianya

我在代码方面经验不足,有一个关于我的 GCSE 计算机科学控制评估的问题。我已经走了很远,只是最后一个障碍阻碍了我。 这个任务要求我使用一个以前制作的简单文件压缩系统,并“开发一个程序,[基于它]来压缩一个带有几个句子的文本文件,包括标点符号。该程序应该能够将一个文件压缩成一个列表单词和位置列表来重新创建原始文件。它还应该能够获取压缩文件并重新创建原始文件的全文,包括标点符号和大写字母。”

到目前为止,我已经可以使用我的第一个程序将所有内容存储为文本文件:

sentence = input("Enter a sentence: ")   
sentence = sentence.split()   
uniquewords = []   
for word in sentence:   
    if word not in uniquewords:   
        uniquewords.append(word)   
 
positions = [uniquewords.index(word) for word in sentence]   
recreated = " ".join([uniquewords[i] for i in positions])   
 
print (uniquewords)   
print (recreated)   
 
positions=str(positions)   
uniquewords=str(uniquewords)   
 
positionlist= open("H:\Python\ControlledAssessment3\PositionList.txt","w")   
positionlist.write(positions)   
positionlist.close   
 
wordlist=open("H:\Python\ControlledAssessment3\WordList.txt","w",)   
wordlist.write(uniquewords)   
wordlist.close   

这使得一切都成为列表,并将它们转换为字符串,以便可以写入文本文档。现在,程序 2 就是问题所在:

uniquewords=open("H:\Python\ControlledAssessment3\WordList.txt","r") 
uniquewords= uniquewords.read() 
 
positions=open("H:\Python\ControlledAssessment3\PositionList.txt","r") 
positions=positions.read() 
 
positions= [int(i) for i in positions] 
 
print(uniquewords) 
print (positions) 
 
recreated = " ".join([uniquewords[i] for i in positions]) 
 
FinalSentence=  
open("H:\Python\ControlledAssessment3\ReconstructedSentence.txt","w") 
FinalSentence.write(recreated) 
FinalSentence.write('\n') 
FinalSentence.close 

当我尝试运行这段代码时,出现了这个错误:

Traceback (most recent call last): 
File "H:\Python\Task 3 Test 1.py", line 7, in <module> 
positions= [int(i) for i in positions] 
File "H:\Python\Task 3 Test 1.py", line 7, in <listcomp> 
positions= [int(i) for i in positions] 
ValueError: invalid literal for int() with base 10: '[' 

那么,你认为我如何让第二个程序将文本重新编译成句子?谢谢,如果这是一篇冗长的帖子,我很抱歉,我一直在努力让它发挥作用。 我假设这与已转换为字符串(包括方括号、逗号和空格等)的列表有关。那么有没有办法将这两个字符串恢复到它们的原始状态,以便我可以重新创建句子?谢谢。

请您参考如下方法:

所以首先,将positions保存为文字字符串是一个很大的奇怪;您应该保存每个元素(与 uniquewords 相同)。考虑到这一点,像这样:

程序1.py:

sentence = input("Type sentence: ") 
 
# this is a test this is a test this is a hello goodbye yes 1 2 3 123 
 
sentence = sentence.split() 
uniquewords = [] 
for word in sentence: 
    if word not in uniquewords: 
        uniquewords.append(word) 
 
positions = [uniquewords.index(word) for word in sentence] 
 
with open("PositionList.txt","w") as f: 
    for i in positions: 
            f.write(str(i)+' ') 
 
with open("WordList.txt","w") as f: 
    for i in uniquewords: 
            f.write(str(i)+' ') 

程序2.py:

with open("PositionList.txt","r") as f: 
    data = f.read().split(' ') 
positions = [int(i) for i in data if i!=''] 
 
with open("WordList.txt","r") as f: 
    uniquewords = f.read().split(' ') 
 
sentence = " ".join([uniquewords[i] for i in positions]) 
print(sentence) 

位置列表.txt

0 1 2 3 0 1 2 3 0 1 2 4 5 6 7 8 9 10  

WordList.txt

this is a test hello goodbye yes 1 2 3 123