用 python 编写一个 Hangman 程序,在传入一个包含多单词字符串和单单词字符串的文件时遇到了一个问题。
文件:
你好棕色狐狸
狗
猫
水
跳跃
#initialize list
wordList = []
# get and open file
getFile = raw_input("Enter file name: ")
filename = open(getFile, "r")
def readWords(filename):
for line in filename:
# split any multi word line
line.split()
# add line to wordList
wordList.append(line)
然而,wordList 的输出仍然是:
wordList = ['hello brown fox\n', 'dog\n', 'cat\n', 'water\n', 'jump\n']
我正在尝试让“hello Brown Fox”显示为 3 个独立的字符串。
最佳答案
你把这个弄得太复杂了 - 只需 .split
整个文件内容:
with open(getFile, "r") as f:
words = f.read().split()
关于python - 刽子手 : File I/O strings and lists python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13312399/