我創建了一個代碼,要求用戶在純文本文件中輸入位置列表,將用戶在文本文件中輸入的位置保存爲列表,而不是要求用戶在每個位置輸入單詞代表(與位置列表相同的順序)結束重新創建句子。 但我不知道什麼事情:打開並寫入文件
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))
他使用Python3。 '嘗試 - 除了'附加?自從什麼時候追加提出任何異常? – DahliaSR
@DahliaSR'try-except'不適用於'.append()',它適用於'int()' - 在用戶輸入非整數的情況下,它不能被轉換爲int。 (注意,它並不存在「while」字樣下。 此外,更新了Python 3的打印語句。我錯過了! – cmeadows