如何在python中搜索文件中是否存在单词?
我有一个名为“dictionary.txt”的文件,其中按字母顺序包含字典中的单词 我想查一下这本词典中是否有某些单词。 有谁能帮我写代码打开这个文件,然后我写一个单词作为输入,然后我收到一个输出,告诉我这个单词是否在字典文件中 以下是我目前掌握的情况:-如何在python中搜索文件中是否存在单词?,python,python-3.x,file,Python,Python 3.x,File,我有一个名为“dictionary.txt”的文件,其中按字母顺序包含字典中的单词 我想查一下这本词典中是否有某些单词。 有谁能帮我写代码打开这个文件,然后我写一个单词作为输入,然后我收到一个输出,告诉我这个单词是否在字典文件中 以下是我目前掌握的情况:- dicfile = open('dictionary.txt', 'r') word = input('enter a word: ') if word in dicfile: print('true') else:
dicfile = open('dictionary.txt', 'r')
word = input('enter a word: ')
if word in dicfile:
print('true')
else:
print('false')
假设要在文件中查找
x
。您可以这样做:
with open("textfile.txt") as openfile:
for line in openfile:
for part in line.split():
if "x" in part:
print part
else:
print false
`您就快到了,只需要了解更多关于python文件处理的信息 dictionary.txt文件:
bac def ghij kl mano pqrstuv
这是您修改的代码代码
dicfile = open('dictionary.txt', 'r')
file1 = dicfile.read()
file1 = file1.split()
word = input('enter a word: ')
if word in file1:
print('true')
else:
print('false')
输出:
测试用例1
$ python3 test_9.py
enter a word: bac
true
测试用例2
$ python3 test_9.py
enter a word: anyword
false
这是一种使用过程编程的方法
dictionary = open('dictionary.txt', 'r') #Only read
word = input('Enter a word:\n) #input typed on line below this
if word in dictionary:
print('{} is in the dictionary (True)'.format(word))
else:
print('{} is not in the dictionary (False)'.format(word))
这里还有一个:
def in_file(filename, word):
file = open(filename, 'r')
if word in file:
return True
else:
return False
word = input("Enter a word:\n")
in_file('dictionary.txt', word)
对于不区分大小写的结果,可以使用
word.lower()
。希望这能有所帮助。堆栈溢出不是代码编写服务,我们只会在人们试图做某件事而陷入困境时提供帮助,我不会给出确切的解决方案,但我将向您展示如何实现您的目标,剩下的就是您的家庭作业:
@Srikar appalaju的回答很好
#Sample 1 - elucidating each step but not memory efficient
lines = []
with open("C:\name\MyDocuments\numbers") as file:
for line in file:
line = line.strip() #or some other preprocessing
lines.append(line) #storing everything in memory!
#Sample 2 - a more pythonic and idiomatic way but still not memory efficient
with open("C:\name\MyDocuments\numbers") as file:
lines = [line.strip() for line in file]
#Sample 3 - a more pythonic way with efficient memory usage. Proper usage of with and file iterators.
with open("C:\name\MyDocuments\numbers") as file:
for line in file:
line = line.strip() #preprocess line
doSomethingWithThisLine(line) #take action on line instead of storing in a list. more memory efficient at the cost of execution speed.
例如:
with open('dictionary.txt','r') as f:
#for reading line by line
for line in f:
print(line)
#reading whole file once
print(f.read())
现在,如何在文件中签入word:
使用python'in'
运算符
#checking word
if 'some_word' in line:
print(True)
else:
print(False)
现在尝试一些方法,当你陷入困境时,寻求帮助,而不是要求编写代码 很好的解释你能展示你的代码吗?dicfile=open('dictionary.txt','r')word=input('enter a word:')如果dicfile中的word:print('true')否则:print('false')我还是python的初学者,因此非常感谢你的帮助。可能的
with open('dictionary.txt','r') as f:
#for reading line by line
for line in f:
print(line)
#reading whole file once
print(f.read())
#checking word
if 'some_word' in line:
print(True)
else:
print(False)