Python 如何使txt文件的搜索输入不区分大小写
现在代码正在运行,但是如果我不将名字和姓氏的第一个字母大写,代码将返回“error:person not found” 我如何设置它,以便无论用户如何输入搜索,它都将返回请求的数据Python 如何使txt文件的搜索输入不区分大小写,python,Python,现在代码正在运行,但是如果我不将名字和姓氏的第一个字母大写,代码将返回“error:person not found” 我如何设置它,以便无论用户如何输入搜索,它都将返回请求的数据 #Iterate through the lines and save them in a dictionary, with the #full name as the key for line in file: stripped = line.strip() name = stripped[:str
#Iterate through the lines and save them in a dictionary, with the
#full name as the key
for line in file:
stripped = line.strip()
name = stripped[:stripped.index(',')]
d[name] = stripped[stripped.index(',')+1:].strip()
def main():
main()尝试使用:
.capitalize()
例如:
'hello world'.capitalize()
产量
'Hello World'
因此,在您的情况下,您会:
name.capitalize()
然后在地址中查找它。您可以使用lower()
、upper()
或capitalize()
,只要您将所选方法附加到两个变量的末尾
使用lower()
:
name.capitalize()
for i in range(len(addresses)):
addresses[i] = addresses[i].lower() # Converting array values to lowercase
if name.lower() not in addresses: # Seeing if lowercase variable is in lowercase array
print("Error: person not found")
else:
print("Person found")