根据用户的输入编辑文件中的一行 | Python

Edit a line in a file depending on user's input | Python

我正在开发通讯录应用程序以提高我的 python 技能,到目前为止,我已经创建了添加新联系人、查看现有联系人的功能,但我无法使用编辑功能他们,我不知道如何处理这个任务(注意编辑和添加信息是基于用户输入的),目前这个应用程序记录的唯一信息是姓名,phone 号码 and/or 电子邮件(如果用户输入了电子邮件)。

我将联系人存储在单独的文件中,文件名是联系人姓名,联系人是 his/her 信息(第一行始终是 phone 号码,第二行如果存在是电子邮件),我现在假设所有联系人都有 phone 号码 由于我认为编辑功能将类似于添加,这里是添加功能

def add_contact(): 
if question == 'add':
    contact_name = input('Enter the name of the contact you want to add: ')

    join_input = dir_path + "\" + contact_name + ".txt"
    #join_input = os.joinpath(dir_path, contact_name)

    os.makedirs(dir_path, exist_ok=True)

    if os.path.exists(join_input):
        print('this contact is founded')

    else:
            
            while True:   
                contact_number = input("Enter the contact's number: ")
                
                if not contact_number.isdigit():
                    print('Type a number next time.')
                    continue
                
                else:

                    f = open(join_input, "a")
                    f.write('Phone number: ' + contact_number)
                    f.write('\n')
                    f.close()


                    email_print = input('Would you like to add an email address? Type yes or no: ').lower()
                    if email_print == 'yes':
                        contact_email = input("Enter the contact's email: ")

                        f = open(join_input, "a")
                        f.write('Email Adress: ')
                        f.write(contact_email)
                        f.close()
                        
                        print('Contact', contact_name, 'is succesfuly created!')
                        break
                    
                    elif email_print == 'no':
                        print('Contact', contact_name, 'is succesfuly created!')
                        break
                
                    else:
                        continue

这是一个例子运行

Do you want to add, view, or delete contact? Enter add, view or delete:  add
Enter the name of the contact you want to add: test
Enter the contact's number: 0129309123
Would you like to add an email address? Type yes or no: yes
Enter the contact's email: test@gmail.com
Contact test is succesfuly created!

到目前为止,我在 edit_contact 中的进步如下

def edit_contact():
while True:
    if question == 'edit':
        contact_edit = input('Enter the name of the contact you want to add: ')
        
        join_edit = dir_path + "\" + contact_edit + ".txt"
        
        if os.path.exists(join_edit):
            contact_input_delete = input('Do you want to edit phone number, or email adress? Type number, or email: ').lower()
            
            if contact_input_delete == 'number':
                
                with open(join_edit, 'w') as file:
                    data = file.readlines()
                    file.writelines(data)
            
        else:
            print("This contact doesn't exists.")
            continue

如果你想看我的全部功能,你可以在github上查看:Github

由于文件内容总是限制在两行内,您可以重复使用整个 add_contact() 函数,稍作改动,在第一次打开文件时使用“w”参数,f = open(join_input, "w"),这将清除文件中先前存储的任何内容,第二次打开应保留“a”以不清除 phone 数字。当然,您需要做一些外观上的更改(打印消息),无论如何新代码将是:

def edit_contact(): 
    contact_name = input('Enter the name of the contact you want to add: ')
    join_input = dir_path + "\" + contact_name + ".txt"
    os.makedirs(dir_path, exist_ok=True)

    if not os.path.exists(join_input):#we need to reverse the condition here, if the contact is found we can edit it, otherwise we need to skip the use's input
        print('this contact does not exist')
    else:
        while True:   
            contact_number = input("Enter the contact's number: ")
            
            if not contact_number.isdigit():
                print('Type a number next time.')
                continue
                
            else:

                f = open(join_input, "w")
                f.write('Phone number: ' + contact_number)
                f.write('\n')
                f.close()


                email_print = input('Would you like to add an email address? Type yes or no: ').lower()
                if email_print == 'yes':
                    contact_email = input("Enter the contact's email: ")

                    f = open(join_input, "a")
                    f.write('Email Adress: ')
                    f.write(contact_email)
                    f.close()
                        
                    print('Contact', contact_name, 'is succesfuly created!')
                    break
                    
                elif email_print == 'no':
                    print('Contact', contact_name, 'is succesfuly created!')
                    break
                
                else:
                    continue