我有一個文本文件(test.txt的),其中包含Python的 - 保持命令窗口打開看到的結果
text1 text2 text text text
下面是我的代碼:
import codecs
BOM = codecs.BOM_UTF8.decode('utf8')
name = (raw_input("Please enter the name of the file: "))
with codecs.open(name, encoding='utf-8') as f:
words=[] #define words here
for line in f:
line = line.lstrip(BOM)
words.extend(line.split()) #append words from each line to words
if len(words) > 2:
print 'There are more than two words'
firstrow = words[:2]
print firstrow #indentation problem here
elif len(words) <2: #use if
print 'There are under 2 words, no words will be shown'
raw_input("Press return to close this window...")
當我運行.py文件,我想讓命令窗口保持打開狀態,以便我可以看到所有打印件,但由於某種原因,它會立即關閉,當我在shell中運行它時,它會起作用。對於某些推理,raw_input不像我通常那樣工作。我的第二天在python,所以我仍然是一個新手!
在此先感謝幫助
機會是你的原始輸入之前的某個地方有一個錯誤... –