2017-03-31 178 views
0

我創建了一個代碼,要求用戶在純文本文件中輸入位置列表,將用戶在文本文件中輸入的位置保存爲列表,而不是要求用戶在每個位置輸入單詞代表(與位置列表相同的順序)結束重新創建句子。 但我不知道什麼事情:打開並寫入文件

1)我怎樣才能使純文本文件彈出只有當前一個關閉(所以只有當文件:list_of_numbers已關閉,其他文件list_of_words將彈出)。

2)如何將輸出寫入純文本文件。

這裏是代碼:

import subprocess 
subprocess.Popen(["notepad","list_of_numbers.txt"]) 
with open("list_of_numbers.txt","r") as pos_file: 
    positions = pos_file.read().spllit() 

subprocess.Popen(["notepad","list_of_words.txt"]) 
with open("list_of_words.txt","r") as sentence_file: 
    words = sentence_file.read().split() 

mapping = dict(zip(positions, words)) 
output = [mapping[position] for position in positions] 

print(' '.join(output)) 

回答

0

林不知道你正在嘗試做的完全是,但是這應該幫助。你並不需要使用子進程,而事實上它可能是一些過於複雜的事情......

輸入:

Enter a number. (Leave blank to continue): 1 
Enter a number. (Leave blank to continue): 2 
Enter a number. (Leave blank to continue): 3 
Enter a number. (Leave blank to continue): 


Enter a word. (Leave blank to continue): a 
Enter a word. (Leave blank to continue): b 
Enter a word. (Leave blank to continue): c 
Enter a word. (Leave blank to continue): 

輸出(控制檯和OUTPUT.TXT):

1, a 

2, b 

3, c 

代碼:

#!/usr/bin/env python 

while True: 

    positions = [] 
    words = [] 

    while True: 
     number = raw_input("Enter a number. (Leave blank to continue): ") 
     if not number: 
      break 

     try: 
      positions.append(int(number)) 
     except TypeError: 
      print("Invalid number provided. Try again.") 

    print("\n") 

    while True: 
     word = raw_input("Enter a word. (Leave blank to continue): ") 
     if not word: 
      break 

     words.append(word) 

    if len(positions) != len(words): 
     print("You did not enter the same number of words and positions..") 
     print("Clearing previous inputs..") 
    else: 
     break 

with open("output.txt", 'a') as output_file: 
    for x in xrange(0, len(positions)): 
     line = "{0}, {1}\n".format(positions[x], words[x]) 
     output_file.write(line) 
     print(line) 
+0

他使用Python3。 '嘗試 - 除了'附加?自從什麼時候追加提出任何異常? – DahliaSR

+0

@DahliaSR'try-except'不適用於'.append()',它適用於'int()' - 在用戶輸入非整數的情況下,它不能被轉換爲int。 (注意,它並不存在「while」字樣下。 此外,更新了Python 3的打印語句。我錯過了! – cmeadows

0

通過查閱Python文檔瞭解如何實現這一點並不成問題。
- Popen
- Reading and Wirting Files


不失! - 如果你創建一個對象,那麼你應該把它分配給一個變量!

# DO 
process = subprocess.Popen(["notepad","list_of_numbers.txt"]) 

# DONT 
subprocess.Popen(["notepad","list_of_numbers.txt"]) 

諮詢Python文檔應該帶你到這樣一個解決方案:

import subprocess 

# don't do anything unless child process has been terminated 
with subprocess.Popen(["notepad","list_of_numbers.txt"]) as process: 
    pass # you could leave this line blank but using pass clearer 

with open("list_of_numbers.txt","r") as pos_file: 
    positions = pos_file.read().split() 

process = subprocess.Popen(["notepad","list_of_words.txt"]) 

with open("list_of_words.txt","r") as sentence_file: 
    words = sentence_file.read().split() 

mapping = dict(zip(positions, words)) 
output = [mapping[position] for position in positions] 

# open a new file in write mode, write data and close the file 
with open('new_file.txt', 'w') as f: 
    f.write(' '.join(output)) 

注意:您仍然需要修復你的任務,以mappingoutput在17行和18,因爲它們不會產生預期的結果,但這不是你問題的一部分。