2015-12-03 39 views
0

我有這段代碼來找到第一次出現這個詞的位置,並將其替換爲實際的程序。 我已經嘗試過這種這段代碼的輸出需要發送到一個單獨的文本文件,爲什麼沒有工作

sentence = "ask not what you can do for your country ask what your country can do for you" 
listsentence = sentence.split(" ") 
d = {} 
i = 0 
values = [] 
for i, word in enumerate(sentence.split(" ")): 
    if not word in d: 
     d[word] = (i + 1) 
    values += [d[word]] 
print(values) 



example = open('example.txt', 'wt') 
example.write(str(values)) 

example.close() 

我怎樣寫這個輸出到一個單獨的文本文件,如記事本。

+0

記事本是一個應用程序,而不是一個文本文件... – poke

回答

0

實際上你的代碼工作 - 每次運行這個程序時都會創建example.txt。你可以在你的目錄中檢查這個文件是否存在。

如果你想在你的腳本關閉後立即打開它補充:

import os 
os.system("notepad example.txt") 
相關問題