Внесены некоторые изменения в ваш код,
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']
users_list = []
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
users_list.append(the_list[pos].split()[0]) # print all users
print(users_list)
Вывод:
['username', 'Hilton', 'privilege', '15', 'password', '0', '$xxxxxxxxxxxxx', 'username', 'gooduser', 'password', '0', '$xxxxxxxxxxxxx', 'username', 'jason', 'secret', '5', '$xxxxxxxxxxxxx']
['Hilton', 'gooduser', 'jason']
Другое решение: (лучший способ)
with open('config_file.txt', 'r') as f:
data = f.read().split()
user_names = [data[i+1] for i,line in enumerate(data) if 'username' in line ]
Выход:
['Hilton', 'gooduser', 'jason']