我有一个文本文件(test.txt),其中包含
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 的第二天,所以我还是个新手!
在此先感谢您的帮助