根据用户的输入|在文件中编辑一行。 Python
我正在研究联系书申请以提高我的Python技能,到目前为止,我创建了添加新联系人的功能,查看现有联系人,但我陷入了编辑功能上,我不知道该如何解决此任务(请注意,编辑和添加信息基于用户输入),当前该应用程序录制的唯一信息是名称,电话号码和/或电子邮件(如果用户输入电子邮件)。
我正在将联系人存储在单个文件中,其中文件名是联系人名称,并且联系人是他/她的信息(第一行是电话号码,第二行(如果是电子邮件)),我现在认为所有联系人都有电话号码 由于我认为编辑函数将与添加相似,这是添加函数
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: [email protected]
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
I'm working on a contact book application to improve my python skill, so far I've created functions to add new contact, view existing contact, but I'm stuck on a function to edit them, I don't know how to tackle this task (note that editing and adding information is based on user input), currently the only information this application is recording are name, phone number and/or email (if user entered an email).
I'm storing the contacts in individual files, where file name is the contact name, and the contact are his/her information (first line is always the phone number, second line if present is the email) and I'm supposing for now that all contact have phone numbers
As I think that the edit function will be similar to the add, here is the add function
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
and here is an example of it running
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: [email protected]
Contact test is succesfuly created!
My progress so far in edit_contact is the following
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
if you want to see my whole function, you can check it on github: Github
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于文件内容始终仅限于两行,因此您可以重复使用整个add_contact()函数,并进行较小的更改,在文件的第一个打开中,使用“ w”参数,f = open(join_input,“ w”) ,这将清除以前存储在文件中的内容,第二个开放应保持“ A”,以免清除电话号码。当然,您需要进行一些化妆品更改(打印消息),无论如何,新代码将是:
Since the file content will always be limited to two lines, you can reuse the whole add_contact() function, with a minor change, in the first open of the file use "w" argument, f = open(join_input, "w"), this will clear whatever is stored previously in the file, the second open should stay "a" to not clear the phone number. And of course you need to do some cosmetic changes (print messages), anyways the new code will be: