【问题标题】:Recreating a sentence using text files and lists使用文本文件和列表重新创建句子
【发布时间】:2016-03-22 21:00:32
【问题描述】:

我对 Python 比较陌生,我目前正在开发一个压缩程序,该程序使用包含单词在列表中的位置和组成句子的单词列表的列表。到目前为止,我已经在两个函数中编写了我的程序,第一个函数; 'compression',获取组成句子的单词和这些单词的位置。我的第二个函数叫做'recreate',这个函数使用他的列表来重新创建句子。然后将重新创建的句子存储在一个名为 recreate.txt 的文件中。我的问题是单词的位置和组成句子的单词没有被写入它们各自的文件,并且没有创建和写入“重新创建”文件。任何帮助将不胜感激。谢谢:)

sentence = input("Input the sentence that you wish to be compressed") 
sentence.lower()
sentencelist = sentence.split() 
d = {} 
plist = [] 

wds = []
def compress():     
    for i in sentencelist:
        if i not in wds:
            wds.append(i)
    for i ,j in enumerate(sentencelist):
        if j in (d):  
            plist.append(d[j]) 
        else:
            plist.append(i)      
    print (plist) 

    tsk3pos = open ("tsk3pos.txt", "wt")    

    for item in plist:
        tsk3pos.write("%s\n" % item)    

    tsk3pos.close()                     
    tsk3wds = open ("tsk3wds.txt", "wt")    

    for item in wds:
        tsk3wds.write("%s\n" % item)    
    tsk3wds.close() 

    print (wds) 

def recreate(compress): 
    compress()
    num = list()
    wds = list()
    with open("tsk3wds.txt", "r") as txt: 
        for line in txt:
            words += line.split()

    with open("tsk3pos.txt", "r") as txt:
        for line in txt:
            num += [int(i) for i in line.split()]



    recreate = ' '.join(words[pos] for pos in num)

    with open("recreate.txt", "wt") as txt:
        txt.write(recreate) 

已更新 我已经修复了所有其他问题,除了 recreate 函数,它不会创建“recreate”文件,也不会用单词重新创建句子,虽然 它使用位置重新创建句子。

def recreate(compress): #function that will be used to recreate the compressed sentence.
    compress()
    num = list()
    wds = list()

    with open("words.txt", "r") as txt: #with statement opening the word text file
        for line in txt: #iterating over each line in the text file.
            words += line.split() #turning the textfile into a list and appending it to num

    with open("tsk3pos.txt", "r") as txt:
        for line in txt:
            num += [int(i) for i in line.split()]

    recreate = ' '.join(wds[pos] for pos in num)

    with open("recreate.txt", "wt") as txt:
        txt.write(recreate) 

    main()



def main():
    print("Do you want to compress an input or recreate a compressed input?")
    user = input("Type 'a' if you want to compress an input. Type 'b' if you wan to recreate an input").lower()
    if user not in ("a","b"):
        print ("That's not an option. Please try again")
    elif user == "a":
        compress()
    elif user == "b":
        recreate(compress)
    main()

main()

【问题讨论】:

  • 首先,不要使用variable = open(file),使用with 上下文管理器。你好像来回切换...
  • 感谢您的回复。我已将所有 variable = open(file) 更改为“with”上下文管理器,并且检查了我的代码,但仍然无法找出任何问题。 @MattDMo
  • “在此处输入代码”是什么意思?您是否还有其他代码未显示?因为目前您正在定义函数但从未调用它们。
  • 另外,您的sentence.lower() 没有被分配给任何东西。
  • 感谢您的回复。输入代码不应该放在那里@user234461

标签: python


【解决方案1】:

一种更简单(但效率较低)的方法:

 recreate_file_object  =  open ( "C:/FullPathToWriteFolder/recreate.txt"   ,   "w"   )
 recreate_file_object.write ( recreate ) 
 recreate_file_object.close ( )

【讨论】:

  • 感谢您的回复。这是否应该解决未创建重新创建文件的问题? @RasikhJ
  • 非常感谢。我会试一试@RasikhJ
猜你喜欢
  • 1970-01-01
  • 2013-07-27
  • 2019-01-12
  • 2013-10-23
  • 1970-01-01
  • 2021-11-22
  • 2013-04-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多