【发布时间】:2021-12-11 18:34:19
【问题描述】:
我正在尝试读取包含数百个名称的 .txt 文件,并且我想查找特定名称。
例如,我有几个类似的名字
John simons
kumar ayush singh
peter calpidi
david brown
等,我想寻找一个子字符串 ayush 如果子字符串存在我想返回完整的字符串,如kumar ayush singh。
我尝试过这样的事情:
try:
with open('D:\\Any One\\name_list.txt') as names:
for row in names:
if row.find(request.strip()) != -1:
results = row
print(row)
except Exception as err:
print(err)
但出现错误:
'charmap' codec can't decode byte 0x81 in position 4467: character maps to undefined
【问题讨论】: