2016-11-25 58 views
1

我需要帮助我的Python代码。我一直试图将输入的文本保存到文本文件中,而不用重复文件中的文字。我不知道该怎么做。Python代码 - 不确定文本文件

任何帮助表示赞赏。

这是我的代码:

import sys 

#user-friendly, informs the user what do to 
answer = input("What is your name?\n") 
print("Hello " + answer + " and welcome to this program!\n") 
print("This program will ask you for a sentence and print out the positions of the words instead of the actual words. it will then save it in a file with the sentence.\n") 

repeat = True 
loop = True 
true = True 

#Allows the user to decide whether or not they want to use the program 
while repeat: 
    answer2 = input("Do you want to do run this program?\n") 
    #if the answer is 'no' then the program stops 
    if answer2.lower() == "No" or answer2.lower() == "nah" or answer2.lower() == "no" or answer2.lower() == "n": 
     print ("Okay then ... Bye.") 
     sys.exit() 
    #if the answer is 'yes' then the code continues 
    elif answer2 == "Yes".lower() or answer2.lower() == "yeah" or answer2.lower() == "yes" or answer2.lower() == "y": 
     print ("Okay then ... \n") 
     while true: 
      if loop == True: 
       sentence = input("Please enter a sentence:\n").lower() 
      #converts the sentence into a list 
      s = sentence.split() 
      #works out the positions of the words 
      positions = [s.index(x)+1 for x in s] 
      print(positions) 

      #opens a text file 
      fi = open("CA Task 2.txt", "w") 
      #Allows you to write over the original content in the file 
      fi.write(str(s)) 
      #it closes the file once you've finished with it 
      fi.close() 

      #opens a text file 
      fi = open("CA Task 2.txt", "a") 
      #Allows you to add to the text file instead of writing over it 
      fi.write("\n") 
      fi.write(str(positions)) 
      #it closes the file once you've finished with it 
      fi.close() 
      sys.exit() 

    #if the answer is neither 'yes' nor 'no' then the programs jumps to this part and allows the user to try again 
    else: 
     print("Please enter a valid answer! Try again!\n") 

让我们只说了一句输入的是“不要问你的国家能为你做,但你能为你的国家做”。

应该拿出说: 不要问你的国家能为你做,但你能为你的国家

[1,2,3,4,5,6,7,8,9做,10,3,9,6,7,8,4,5]

这个工作,然后它必须被保存到一个文本文件: ['问''''','什么'你的','国家','可以','做','为','你','但','是','你','可以','做','为',' ,'country']

[1,2,3,4,5,6,7,8,9,10,3,9,6,7,8,4,5]

这很好,但我想要它做的是不重复单词,如果它已经在文本文件中提到过一次。

+0

不太清楚你问什么帮助,在这里。你在期待什么 - 可能是一个用户输入的例子,以及将保存在文件中的东西会很方便。目前正在发生什么。 – freebie

+0

基本上我想用户输入一个句子(这个作品),然后我想将它保存在一个文本文件的位置(这也适用)。但是,我不知道该怎么办,如果一句话重复在句子中,你怎么保持这样的文本文件@freebie –

+0

@AsifKhan,你可以在你的问题中添加你的预期答案?它可以是图像或文字,这将有助于理解你的问题。 –

回答

3

有内置叫做功能:sethttps://docs.python.org/3/library/stdtypes.html#set

import sys 

#user-friendly, informs the user what do to 
answer = input("What is your name?\n") 
print("Hello " + answer + " and welcome to this program!\n") 
print("This program will ask you for a sentence and print out the positions of the words instead of the actual words. it will then save it in a file with the sentence.\n") 

repeat = True 
loop = True 
true = True 

#Allows the user to decide whether or not they want to use the program 
while repeat: 
    answer2 = input("Do you want to do run this program?\n") 
    #if the answer is 'no' then the program stops 
    if answer2.lower() == "No" or answer2.lower() == "nah" or answer2.lower() == "no" or answer2.lower() == "n": 
    print ("Okay then ... Bye.") 
    sys.exit() 
#if the answer is 'yes' then the code continues 
elif answer2 == "Yes".lower() or answer2.lower() == "yeah" or answer2.lower() == "yes" or answer2.lower() == "y": 
    print ("Okay then ... \n") 
    while true: 
     if loop == True: 
      sentence = input("Please enter a sentence:\n").lower() 
     # converts the sentence into a list 
     s = sentence.split() 
     # for loop makes sure that if the word is in the list then it wont print it out again 
     for word in s: 
      if word not in s: 
       s.append(word) 
     # works out the positions of the words 
     positions = [s.index(x) + 1 for x in s] 
     print(set(positions)) 

     # opens a text file 
     fi = open("CA Task 2.txt", "w") 
     # Allows you to write over the original content in the file 
     fi.write(str(set(s))) 
     # it closes the file once you've finished with it 
     fi.close() 

     # opens a text file 
     fi = open("CA Task 2.txt", "a") 
     # Allows you to add to the text file instead of writing over it 
     fi.write("\n") 
     fi.write(str(set(positions))) 
     # it closes the file once you've finished with it 
     fi.close() 
     sys.exit() 

     #if the answer is neither 'yes' nor 'no' then the programs jumps to this part and allows the user to try again 
     else: 
     print("Please enter a valid answer! Try again!\n")' 
0

此:

for word in s: 
    if word not in s: 
     s.append(word) 

没有道理给我。你想创建一个独特的单词列表吗?它给你相同的清单。

if answer2.lower() == "No"是superfluos,因为结果永远不会'不'。

比方说,你从一个句子,其中有些话是唯一一个列表,有些不是: s = ['foo', 'bar', 'foo', 'foo', 'spam'] 和你想获得的这些独特单词数字表示,你可以得到它这样的:

d = [] 
i = 0 
for item in s: 
    if item not in s[:i]: 
     d.append(i) 
     i += 1 
    else: 
     d.append(s.index(item)) 

现在你会得到一个列表,其中每个数字是在的话的唯一表示:

[0, 1, 0, 0, 2] 
2

所以这是你的,这不是行为环路部分?在我看来,你正在将一个句子分成一个列表,所以['here', 'is', 'my', 'sentence', 'input'],然后遍历每个这些单词,并追加到列表中,如果它们不在其中。所以这对s不会有任何影响。

Python有一个set集合,它拥有唯一值。所以它就像一个list但不会让你添加重复。你可以使用它来代替你的for循环,因为你可以用list来初始化一个set--就像你用split()调用创建的那个一样。

s = sentence.split() 
s = set(s) 

编辑:集不保留顺序像一个list。因此,如果按照第一次出现的顺序保存这些词很重要,那么这种方法将不起作用。

0

更改检查单词是否在s中的部分。您应该将您的单词保存在另一个列表中,并检查该单词是否已经在另一个列表中。就像下面的代码:

#for loop makes sure that if the word is in the list then it wont print it out again 
    new_s = [] 
    for word in s: 
     if word not in new_s: 
      new_s.append(word) 
+0

我试过这个,但它似乎没有工作 –