我正在尝试将用户名从配置文件提取到一个列表中,并将用户名与另一个安全的用户名列表进行比较。 配置文件如下所示:
username Hilton privilege 15 password 0 $xxxxxxxxxxxxx
username gooduser password 0 $xxxxxxxxxxxxx
username jason secret 5 $xxxxxxxxxxxxx
输出的问题不是一个列表!(每个用户都在一个列表中)
['Hilton']
['gooduser']
['jason']
我正在把文件读成一个单子。 然后定位“username”位置并使用enumerate查找该位置
the_list = []
with open('config_file.txt', "r") as f:
the_list = f.read().split()
print(the_list)
find_keyword = 'username'
secure_users = ['jason','test','admin']
for i,x in enumerate(the_list): # search in the list
if x=='username': # for this keyword 'username'
pos = i + 1 # position of every username
print(the_list[pos].split()) # print all users.
#Compare secure_users[] vs the_list[] here
预期的输出是一个列表,如>;>;['Hilton'、'gooduser'、'jason']
这样我就可以把它和安全用户列表进行比较
对你的代码做了一些修改
输出:
另一种解决方案:(最佳方式)
输出:
请尝试以下操作:
使用正则表达式。你知道吗
例如:
相关问题 更多 >
编程相关推荐