我如何查找整个单词是否在文本文件中?
How do i find if a whole word is in a text file?
我的代码如下所示:
file = open('names.txt', 'r')
fileread = file.read()
loop = True
while loop is True:
with open('names.txt', 'r') as f:
user_input = input('Enter a name: ')
for line in f:
if user_input in line:
print('That name exists!')
else:
print('Couldn\'t find the name.')
该代码主要是询问用户名称,如果该名称存在于文本文件中,则代码会说它存在,但如果不存在,则表示无法找到它。
我唯一的问题是,如果您只输入部分名称,它会告诉您整个名称都存在。例如,我的文本文件中的名字是:Anya、Albert 和 Clemont,它们都在不同的行中分开。如果我在提示输入 user_input 时输入 'a',代码仍会说该名称存在,并且只会要求输入另一个名称。我明白为什么要这样做,因为 'a' 在技术上是行内的,但我如何做到这一点,以便它只说名称存在,如果他们输入整个东西?总的来说,我的意思是他们输入例如 'Anya',而不是 'a',并且代码仅在他们输入 'Anya' 时说该名称存在。谢谢
要回答这个问题,就做相等比较。还注意到你有无限循环,这是预期的吗?当在文件
中找到匹配的名称时,我更改了代码以退出该循环
file = open('inv.json', 'r')
fileread = file.read()
loop = True
while loop is True:
with open('inv.json', 'r') as f:
user_input = raw_input('Enter a name: ')
for line in f:
if user_input == line.strip():
print('That name exists!')
break
#loop =False
else:
print('Couldn\'t find the name.')
输入
Anya
Albert
Clemont
输出
Enter a name: an
Couldn't find the name.
Couldn't find the name.
Couldn't find the name.
Enter a name: An
Couldn't find the name.
Couldn't find the name.
Couldn't find the name.
Enter a name: Any
Couldn't find the name.
Couldn't find the name.
Couldn't find the name.
Enter a name: Anya
That name exists!
使用 re.seach() 函数的简短解决方案:
import re
with open('lines.txt', 'r') as fh:
contents = fh.read()
loop = True
while loop:
user_input = input('Enter a name: ').strip()
if (re.search(r'\b'+ re.escape(user_input) + r'\b', contents, re.MULTILINE)):
print("That name exists!")
else:
print("Couldn't find the name.")
测试用例:
Enter a name: Any
Couldn't find the name.
Enter a name: Anya
That name exists!
Enter a name: ...
我的代码如下所示:
file = open('names.txt', 'r')
fileread = file.read()
loop = True
while loop is True:
with open('names.txt', 'r') as f:
user_input = input('Enter a name: ')
for line in f:
if user_input in line:
print('That name exists!')
else:
print('Couldn\'t find the name.')
该代码主要是询问用户名称,如果该名称存在于文本文件中,则代码会说它存在,但如果不存在,则表示无法找到它。
我唯一的问题是,如果您只输入部分名称,它会告诉您整个名称都存在。例如,我的文本文件中的名字是:Anya、Albert 和 Clemont,它们都在不同的行中分开。如果我在提示输入 user_input 时输入 'a',代码仍会说该名称存在,并且只会要求输入另一个名称。我明白为什么要这样做,因为 'a' 在技术上是行内的,但我如何做到这一点,以便它只说名称存在,如果他们输入整个东西?总的来说,我的意思是他们输入例如 'Anya',而不是 'a',并且代码仅在他们输入 'Anya' 时说该名称存在。谢谢
要回答这个问题,就做相等比较。还注意到你有无限循环,这是预期的吗?当在文件
中找到匹配的名称时,我更改了代码以退出该循环file = open('inv.json', 'r')
fileread = file.read()
loop = True
while loop is True:
with open('inv.json', 'r') as f:
user_input = raw_input('Enter a name: ')
for line in f:
if user_input == line.strip():
print('That name exists!')
break
#loop =False
else:
print('Couldn\'t find the name.')
输入
Anya
Albert
Clemont
输出
Enter a name: an
Couldn't find the name.
Couldn't find the name.
Couldn't find the name.
Enter a name: An
Couldn't find the name.
Couldn't find the name.
Couldn't find the name.
Enter a name: Any
Couldn't find the name.
Couldn't find the name.
Couldn't find the name.
Enter a name: Anya
That name exists!
使用 re.seach() 函数的简短解决方案:
import re
with open('lines.txt', 'r') as fh:
contents = fh.read()
loop = True
while loop:
user_input = input('Enter a name: ').strip()
if (re.search(r'\b'+ re.escape(user_input) + r'\b', contents, re.MULTILINE)):
print("That name exists!")
else:
print("Couldn't find the name.")
测试用例:
Enter a name: Any
Couldn't find the name.
Enter a name: Anya
That name exists!
Enter a name: ...