简体   繁体   English

如何从文本文件中读取多行并选择要编辑的行

[英]How to read multiple lines from a text file and choose one to edit

I am a university student and have to prepare a lesson in Python to give to a class of year 9's. 我是一名大学生,必须准备使用Python上一堂课才能上9年级。 I have created an address book program which allows them to create an address book and add entries and view the book. 我创建了一个通讯录程序,该程序允许他们创建通讯录并添加条目和查看该通讯录。

The one part I can't figure out is how to edit entries. 我不知道的一部分是如何编辑条目。 You basically have to be able to pick one out of several lines and then type new data which will save over the original line. 基本上,您必须能够从几行中选择一个,然后键入新数据,这将保存在原始行上。

When adding entries originally, I take in the name, age, address and city in separate variables and then write them to a text file with commas between them. 最初添加条目时,我将名称,年龄,地址和城市输入到单独的变量中,然后将它们写入文本文件,并用逗号分隔。

I wasn't sure what information to give over just yet as I don't usually use Stack Overflow so if you need any more information or code please just let me know! 我不确定要提供什么信息,因为我通常不使用Stack Overflow,所以如果您需要更多信息或代码,请告诉我!

Thank you! 谢谢! David 大卫

Edit I've added all the code below. 编辑我已经添加了下面的所有代码。 It prints to a text file and separates them by commas. 它打印到一个文本文件,并用逗号分隔。

def createAddBook():
f = open("address_book.txt", "w")

def viewAddBook():

f = open("address_book.txt", "r")

contacts = f.read()

print(contacts)
print()

f.close

def addEntry():
addList = []

Name = input("What is the name?" + "\n")
Age = input("What is the age?" + "\n")
Address = input("What is the address?" + "\n")
City = input("What is the city?" + "\n")

addList.append(Name + ", ")
addList.append(Age + ", ")
addList.append(Address + ", ")
addList.append(City + "\n")

f = open("address_book.txt", "a")

for entry in addList:
    f.write(entry)

f.close()



inuse = True
while inuse == True:
choice = input("Do you have an address book? (yes/no)" + "\n")
if choice == "yes":
    choice = input("Would you like to view, add or edit your address book? (view/add/edit)" + "\n")
    if choice == "view":
        viewAddBook()
    elif choice == "edit":
         editEntry()
    elif choice == "add":
        addEntry()

elif choice == "no":
    createAddBook()
    print("Address book has been created!")

    choice = input("Would you like to add an entry to the address book? (yes/no)"  + "\n")
    if choice == "yes":
        addEntry()
    elif choice == "no":
        inuse = False
elif choice == "close":
    break

I understand that you want to create your address book as a CSV file (store one entry per line, and use commas to separate the fields of a given entry). 我知道您想将您的通讯录创建为CSV文件(每行存储一个条目,并使用逗号分隔给定条目的字段)。 Is that your goal? 那是你的目标吗? To manage such a file, the easiest solution is to use the csv module from the standard Python library. 要管理此类文件,最简单的解决方案是使用标准Python库中的csv模块。 Here is a link to the documentation page: 这是文档页面的链接:

https://docs.python.org/3/library/csv.html#examples https://docs.python.org/3/library/csv.html#examples

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

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