如何使用python在文本文件中获得每个键和值



我在使用python理解文本文件中的键和值的概念时有两个问题。

第一个:

我有这个名为login.txt的文件,它有几个键和值,例如:

{'ID':1, 'Username':'x', 'Password':'123','Role':'Immigration officer'}
{'ID':2, 'Username':'y', 'Password':'321','Role':'Customs officer'}

这是我的登录代码,它工作,但它仍然循环每次我登录用户名和密码。但我需要它一旦我登录任何正确的值在txt文件中直接停止循环,我不知道我是否理解这个概念或不。下面是代码:

def login(userinput, passinput):
with open(login_db, 'r') as f:
for single_line in f:
dict_to_check = ast.literal_eval(single_line)
username = dict_to_check['Username']
password = dict_to_check['Password']
role = dict_to_check['Role']
try:
if userinput == username:
if passinput == password:
if role == 'Immigration officer':
Immigration()
elif role == 'Customs officer':
Customs()
else:
print('Incorrect role you do not have the authority.')
else:
print('Password is incorrect.') #It still looping here
else:
print('Incorrect username')
except:
print("An exception occurred")

第二个问题是:我有另一个名为passenger.txt的txt文件,它有以下内容:

{'Civil ID':'123', 'Name':'John', 'DOB':'12/12/2000', 'Gender':'Male', 'Customs fine':'', 'Status':''}
{'Civil ID':'1010', 'Name':'Sara', 'DOB':'6/10/2000', 'Gender':'Female', 'Customs fine':'', 'Status':''}

我有一个代码选择一个特定的列与ID之后,我想修改这个ID的Status值。下面是代码:

def Existing_passenger(ID):
with open(passenger_db, 'r') as f:
for single_line in f:
dict_to_check = ast.literal_eval(single_line)
civil_id = dict_to_check['Civil ID']
name = dict_to_check['Name']
dob = dict_to_check['DOB']
gender = dict_to_check['Gender']
customs_fine = dict_to_check['Customs fine']
status = dict_to_check['Status']
try:
if ID == civil_id:
print("-Passenger's information-")
print('Name: '+name+'nDate of birth: '+dob+'nGender: '+gender+'nCustoms fine: '+customs_fine+'nStatus: '+status+'n')
print('A. Arrival ApprovednB. Arrival RejectednC. Departure ApprovednD. Departure RejectednE. Go Back to previous menu')
status_input = input('Select one of the following options: ')
if status_input == 'A':
if status == '':
#What should i write here to modify its status value??
elif status_input == 'B':
print('B')
elif status_input == 'C':
print('C')
elif status_input == 'D':
print('D')
elif status_input == 'E':
print('E')
else:
print('Wrong entry, please try again.')
except:
print("An exception occurred")

我尝试用python编程语言使用文本文件来理解键和值对的正确概念。

第一个:
要在找到匹配后停止循环,应该在找到正确的登录后添加break语句。例如:

...
if userinput == username:
if passinput == password:
if role == 'Immigration officer':
Immigration()
elif role == 'Customs officer':
Customs()
else:
print('Incorrect role you do not have the authority.')
break  # stop the loop
else:
print('Password is incorrect.')
else:
print('Incorrect username')

第二个:
如果原passenger['Status']的值为'',写入passenger['Status'] = 'Arrival Approved',则passenger['Status']的值将更新为'Arrival Approved'

...
if status_input == 'A':
if status == '':
status = 'Arrival Approved'  # modify the status value
...

对于第一个问题,您需要告诉python在获得正确的值时跳出循环。如:

try:
if userinput == username:
if passinput == password:
if role == 'Immigration officer':
Immigration()
break # This tells python to exit the loop

对于第二个问题,请准确地解释你的代码输出有什么问题。

最新更新