我正在通过麻省理工学院开放式课程的Python编程课程。在这个问题集上,我已经获得了一些代码和文本文件。代码和文本文件位于同一文件夹中。代码如下所示:
import random
import string
def load_words( ):
print "Loading word list from file..."
inFile = open (WORDLIST_FILENAME, 'r', 0)
line = inFile.readline( )
wordlist = string.split (line)
print " ", len(wordlist), "words loaded."
return wordlist
def choose_word (wordlist):
return random.choice (wordlist)
wordlist = load_words ( )
当我按原样运行代码时,问题集指令说我应该得到这个:
Loading word list from file...
55900 words loaded.
出于某种原因,当我运行代码时,我得到了:
Loading word list from file...
1 words loaded
我尝试从open
功能的输入中省略第2和第3个参数,但无济于事。问题是什么?
此外,当我尝试打印wordlist
的值时,我得到了
['AA']
当我在相关函数的上下文中打印line
的值时,我得到:
AA
文本文件的确以'AA'
开头,但是后面的所有字母呢?
答案 0 :(得分:1)
line = inFile.readline( )
应为readlines()
,复数
readline
只会读取一行。只读一个单词的原因
使用readlines()
将为您提供一个由输入文件中的新行字符分隔的列表。
答案 1 :(得分:1)
这样的原始文件:
cat wordlist.txt
aa
bb
cc
dd
ee
像这样的python文件:
import random
def load_words(WORDLIST_FILENAME):
print "Loading word list from file..."
wordlist = list()
# 'with' can automate finish 'open' and 'close' file
with open(WORDLIST_FILENAME) as f:
# fetch one line each time, include '\n'
for line in f:
# strip '\n', then append it to wordlist
wordlist.append(line.rstrip('\n'))
print " ", len(wordlist), "words loaded."
print '\n'.join(wordlist)
return wordlist
def choose_word (wordlist):
return random.choice (wordlist)
wordlist = load_words('wordlist.txt')
然后结果:
python load_words.py
Loading word list from file...
5 words loaded.
aa
bb
cc
dd
ee
答案 2 :(得分:1)
你写的函数可以在一行中读取单词。它假设所有单词都在文本文件中以单行写入,因此读取该行并通过拆分创建列表。但是,您的文本文件似乎也包含一些换行符。因此,您可以用以下内容替换以下内容:
line = inFile.readline( )
wordlist = string.split (line)
使用:
wordlist =[]
for line in inFile:
line = line.split()
wordlist.extend(line)
print " ", len(wordlist), "words loaded."