我正在使用python制作一个简单的信息检索程序,它获取客户端ID和用户的客户端ID是txt文件看起来像每行的前5个字符
RYMAU
ELOWE
RUKUM
KLKID
LAJON
我希望用户输入一个5个字母的ID,并在我想出这个特定客户ID时检查每一行:
clientID = input('Enter your clientID : ')
fob = open('clientRecords.txt', 'r') # opens file stores it in a var called fob and reads from it
if (clientID in fob.readline(5)):
print('Access granted')
else:
print('Access denied')
fob.close()
但是这只检查第一行的前5个字母而不是所有字母。
这就是文本文件的实际外观:
NeQua,High,Running,5,Swimming,40,Aerobics,40,Football,20,Tennis,10
ImKol,Moderate,Walking,40,Hiking,0,Cleaning,40,Skateboarding,30,Basketball,20
YoTri,Moderate,Walking,20,Hiking,30,Cleaning,40,Skateboarding,20,Basketball,40
RoDen,High,Running,20,Swimming,20,Aerobics,40,Football,30,Tennis,50
NaThe,Moderate,Walking,30,Hiking,30,Cleaning,20,Skateboarding,10,Basketball,30
ReWes,Moderate,Walking,30,Hiking,20,Cleaning,50,Skateboarding,40,Basketball,20
BrFre,High,Running,20,Swimming,30,Aerobics,30,Football,30,Tennis,20
KaDat,High,Running,30,Swimming,20,Aerobics,10,Football,20,Tennis,30
ViRil,High,Running,50,Swimming,50,Aerobics,60,Football,40,Tennis,50
TrGeo,High,Running,10,Swimming,20,Aerobics,30,Football,30,Tennis,20
DaWay,High,Running,60,Swimming,50,Aerobics,40,Football,50,Tennis,50
CaAma,High,Running,30,Swimming,20,Aerobics,10,Football,20,Tennis,30
ArRes,High,Running,0,Swimming,10,Aerobics,30,Football,20,Tennis,20
BeVic,High,Running,20,Swimming,20,Aerobics,30,Football,30,Tennis,10
MaFre,High,Running,10,Swimming,20,Aerobics,20,Football,40,Tennis,30
AnMer,Moderate,Walking,40,Hiking,30,Cleaning,30,Skateboarding,20,Basketball,30
SaBro,Moderate,Walking,30,Hiking,20,Cleaning,30,Skateboarding,20,Basketball,20
PoLig,Moderate,Walking,20,Hiking,20,Cleaning,20,Skateboarding,30,Basketball,30
HeZbe,Moderate,Walking,30,Hiking,40,Cleaning,20,Skateboarding,40,Basketball,10
GiLop,Moderate,Walking,40,Hiking,0,Cleaning,30,Skateboarding,40,Basketball,20
DeTur,Moderate,Walking,10,Hiking,0,Cleaning,10,Skateboarding,30,Basketball,30
LaKin,Moderate,Walking,20,Hiking,20,Cleaning,30,Skateboarding,30,Basketball,20
AnVen,Moderate,Walking,50,Hiking,0,Cleaning,50,Skateboarding,50,Basketball,20
LoLew,Moderate,Walking,10,Hiking,20,Cleaning,10,Skateboarding,30,Basketball,40
NyRed,Moderate,Walking,0,Hiking,0,Cleaning,0,Skateboarding,0,Basketball,20
答案 0 :(得分:7)
您需要阅读所有行,并测试每行的前五个字符:
with open('clientRecords.txt') as fob:
allowed_access = any(line.startswith(clientID) for line in fob)
if allowed_access:
print('Access granted')
else:
print('Access denied')
如果文件中的任何行以提供的前缀开头, allowed_access
将为True
,并且一旦找到匹配就会停止检查行。
注意:为避免作弊,您可能需要验证提供的clientID
实际上是五个字符。
如果您想获取与该ID相关联的行,而不仅仅是该ID是否存在,您可以将该检查修改为:
with open('clientRecords.txt') as fob:
access_data = next((line for line in fob if line.startswith(clientID)), None)
if access_data is not None: # clientID exists, and access_data is the line it was found on
此外,如果您要经常执行此测试,并且记录文件很少更改,您可能希望在内存中缓存以减少I / O工作。您需要预先加载有效ID(不在任何验证用户的循环或事件处理程序中):
with open('clientRecords.txt') as fob:
known_users = {line[:5] for line in fob}
然后您的每次登录测试简化为:
if clientID in known_users:
print('Access granted')
else:
print('Access denied')
将立即运行(出于所有意图和目的),而不是在每次测试中重新打开和重新读取文件的有意义的成本。您可能希望过期超时,之后您每隔一段时间重新传输known_users
或os.stat
文件,如果已修改,则会重新缓存。
获取相关行的版本是:
with open('clientRecords.txt') as fob:
known_users = {line[:5]: line for line in fob}
并检查/查找:
access_data = known_users.get(clientID) # Returns None if the clientID is not found, or the complete line if it is found
if access_data is not None:
print('Access granted')
else:
print('Access denied')