繁体   English   中英

如何使用python在文本文件中获取每个键一个单独的值

[英]How to get each key an value separate in text file using 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 Approved\nB. Arrival Rejected\nC. Departure Approved\nD. Departure Rejected\nE. 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 编程语言的文本文件来理解键和值对的正确概念。

对于第一个问题,你需要告诉python当你有正确的值时跳出循环。 例如。

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

对于第二个问题,请准确说明您的代码输出有什么问题。

第一:
要在找到匹配项后停止循环,您应该在找到正确的登录后添加一个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
        ...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM