以特定方式从文件中读取单词
Reading a word from a file in a specific way
所以我试图将单个单词存储到一个文件中(我已经设法弄清楚该怎么做)。然后程序会重复并要求我输入另一个词。它应该检查这个词是否已经存在于文件中(它应该存在)。我知道我已经输入了一个单词并将其存储在文件中但是当我再次输入相同的单词时它没有意识到文件中已经存在该单词。 (这一切都在 def 函数中,所以当我说下次它运行时,我的意思是下次我调用该函数时)
代码如下:
def define():
testedWord = subject
lineNumber = 1
lineInFile = "empty"
exists = False
while lineInFile != "":
wordsFile = open("Words.txt", "a")
lineInFile = linecache.getline("Words.txt", lineNumber)
lineNumber = lineNumber + 1
lineInFile = lineInFile.replace("\n", "")
if lineInFile == subject:
definitionNumber = lineNumber
exists = True
if exists == False:
wordsFile.write(testedWord)
wordsFile.write("\n")
wordsFile.close()
subject = input("")
define()
##This whole thing basically gets repeated
就像我说的,如果我存储了一个新词,然后在同一个程序中尝试再次输入同一个词,那么它不会识别它已经存储了这个词。当我停止程序并重新启动它时,它可以工作(但我不想那样做)
感谢您的帮助(如果可以的话,哈哈)
旦
我认为您(几乎)使所有事情变得比实际需要的更复杂。这是您尝试做的另一种方式:
def word_check(f_name, word):
with open(f_name) as fi:
for line in fi: # let Python deal with line iteration for you
if line.startswith(word):
return # return if the word exists
# word didn't exist, so reopen the file in append mode
with open(f_name, 'a') as fo:
fo.write("{}\n".format(word))
return
def main():
f_name = "test.txt"
with open(f_name, 'w') as fo:
pass # just to create the empty file
word_list = ['a', 'few', 'words', 'with', 'one',
'word', 'repeated', 'few'] # note that 'few' appears twice
for word in word_list:
word_check(f_name, word)
if __name__ == "__main__":
main()
这将生成一个包含以下文本的输出文件:
a
few
words
with
one
repeated
在这个例子中,我只是创建了一个单词列表而不是使用输入来保持例子的简单。不过,请注意您当前的方法效率低下。您正在重新打开文件并阅读输入的每个单词的每一行。考虑在记忆中建立你的单词列表,然后在最后写出来。这是一个利用内置 set 数据类型的实现。他们不允许重复的元素。如果您不介意在程序 运行 的 结尾 而不是即时写出文件,您可以改为这样做:
def main():
word_set = set()
while True:
word = input("Please enter a word: ")
if word == 'stop': # we're using the word 'stop' to break from the loop
break # this of course means that 'stop' should be entered
# as an input word unless you want to exit
word_set.add(word)
with open('test.txt', 'w') as of:
of.writelines("{}\n".format(word) for word in word_set)
# google "generator expressions" if the previous line doesn't
# make sense to you
return
if __name__ == "__main__":
main()
打印输出:
Please enter a word: apple
Please enter a word: grape
Please enter a word: cherry
Please enter a word: grape
Please enter a word: banana
Please enter a word: stop
生成此文件:
grape
banana
cherry
apple
所以我试图将单个单词存储到一个文件中(我已经设法弄清楚该怎么做)。然后程序会重复并要求我输入另一个词。它应该检查这个词是否已经存在于文件中(它应该存在)。我知道我已经输入了一个单词并将其存储在文件中但是当我再次输入相同的单词时它没有意识到文件中已经存在该单词。 (这一切都在 def 函数中,所以当我说下次它运行时,我的意思是下次我调用该函数时)
代码如下:
def define():
testedWord = subject
lineNumber = 1
lineInFile = "empty"
exists = False
while lineInFile != "":
wordsFile = open("Words.txt", "a")
lineInFile = linecache.getline("Words.txt", lineNumber)
lineNumber = lineNumber + 1
lineInFile = lineInFile.replace("\n", "")
if lineInFile == subject:
definitionNumber = lineNumber
exists = True
if exists == False:
wordsFile.write(testedWord)
wordsFile.write("\n")
wordsFile.close()
subject = input("")
define()
##This whole thing basically gets repeated
就像我说的,如果我存储了一个新词,然后在同一个程序中尝试再次输入同一个词,那么它不会识别它已经存储了这个词。当我停止程序并重新启动它时,它可以工作(但我不想那样做)
感谢您的帮助(如果可以的话,哈哈) 旦
我认为您(几乎)使所有事情变得比实际需要的更复杂。这是您尝试做的另一种方式:
def word_check(f_name, word):
with open(f_name) as fi:
for line in fi: # let Python deal with line iteration for you
if line.startswith(word):
return # return if the word exists
# word didn't exist, so reopen the file in append mode
with open(f_name, 'a') as fo:
fo.write("{}\n".format(word))
return
def main():
f_name = "test.txt"
with open(f_name, 'w') as fo:
pass # just to create the empty file
word_list = ['a', 'few', 'words', 'with', 'one',
'word', 'repeated', 'few'] # note that 'few' appears twice
for word in word_list:
word_check(f_name, word)
if __name__ == "__main__":
main()
这将生成一个包含以下文本的输出文件:
a
few
words
with
one
repeated
在这个例子中,我只是创建了一个单词列表而不是使用输入来保持例子的简单。不过,请注意您当前的方法效率低下。您正在重新打开文件并阅读输入的每个单词的每一行。考虑在记忆中建立你的单词列表,然后在最后写出来。这是一个利用内置 set 数据类型的实现。他们不允许重复的元素。如果您不介意在程序 运行 的 结尾 而不是即时写出文件,您可以改为这样做:
def main():
word_set = set()
while True:
word = input("Please enter a word: ")
if word == 'stop': # we're using the word 'stop' to break from the loop
break # this of course means that 'stop' should be entered
# as an input word unless you want to exit
word_set.add(word)
with open('test.txt', 'w') as of:
of.writelines("{}\n".format(word) for word in word_set)
# google "generator expressions" if the previous line doesn't
# make sense to you
return
if __name__ == "__main__":
main()
打印输出:
Please enter a word: apple
Please enter a word: grape
Please enter a word: cherry
Please enter a word: grape
Please enter a word: banana
Please enter a word: stop
生成此文件:
grape
banana
cherry
apple