【问题标题】:Appending a text with an appended dictionary python使用附加字典 python 附加文本
【发布时间】:2019-03-14 10:34:09
【问题描述】:

问题是我想将学生添加到现有记录中,然后使用 python 中的文件将其保存到 txt.file,但是它不会附加到文本文件中。我的代码的预期结果是将两个学生添加到字典中,然后将其保存到一个文本文件中,然后一旦读取该文本文件,添加的学生就会出现。

while True:
def Menu(): #Menu Function

    print("****Student Record****")
    print()
    print("[1] Add Student")
    print("[2] View Student")
    print("[3] View all Students")
    print("[4] Delete a Student")
    print("[5] Delete all Students")
    print("[6] Load file")
    print("[7] Save to file")
    print("[0] Exit")

def save(): #isang student lang na-aadd
    saveHandle = open("record.txt", "a")
    for k in record: #Checks each key in dictionary
        student = k #each student is assigned to the key
        i = 0
        if student == str(k): #checks if student is in dictionary
            saveHandle.write(str(new + ",") 
            for k in record[k]:
                if i == 0:
                    saveHandle.write(str(k))
                if i == 1:
                    saveHandle.write(str(k))
                if i == 2:
                    saveHandle.write(str(k))
                if i == 3:
                    saveHandle.write(str(k))
                i = i + 1
def load():
    record = [] #oks na

    readHandle = open("record.txt", "r")

    for line in readHandle:
        student = line[:-1]
        record.append(student)

    readHandle.close()

    print(record)




Menu()


print() #space

option = int(input("Enter option: "))

print()

    record = {"2018-00000" : ["Jake Peralta", "BA Communication Arts", "1st Year", "19"]}
#Current school record

if option == 1: #Adding a student in the record
    new = input("Enter student no.: ")
    newname = input("Enter name: ")
    newdegree = input("Enter degree: ")
    newyear = input("Enter year level: ")
    newage = input("Enter age: ")

    record[new]=(newname, newdegree, newyear, newage) #adding a new key and list to the dictionary


    print("Student record has been added")

elif option == 2: #View a student's record
    student = input("Enter the Student no. of the student you want to view: ")
    print()
    for k in record: #checks each key in dictionary
        i = 0
        if student == k: #checks if user input student no. is equal to any key in the dictionary
            print("Student no:",student)
            for k in record[k]: #checks each element in the list per key
                if i == 0: #first element
                    print("Name:",k)
                if i == 1: #second element
                    print("Degree:",k)
                if i == 2: #third element
                    print("Year Level:",k)
                if i == 3: #fourth element
                    print("Age:",k)
                i = i + 1 #updates each item in list    



elif option == 3: #View all students
    for k in record: #Checks each key in dictionary
        student = k #each student is assigned to the key
        i = 0
        if student == str(k): #checks if student is in dictionary
            print("Student no:",student)
            for k in record[k]: #for every item in list per key
                if i == 0:
                    print("Name:",k)
                if i == 1:
                    print("Degree:",k)
                if i == 2:
                    print("Year Level:",k)
                if i == 3:
                    print("Age:",k)
                i = i + 1
            print()
    else:
        print("No Students on the record")



elif option == 4: #Deleting a student
    xstudent = input("Enter the student no. of the student you want to delete: ")
    print()
    for k in record: #Checks each key in dictionary
        if xstudent == str(k):#Checks if user input is in dictionary
            deleted = record.pop(k) #deletes the key and value
            print("Student record has been deleted.")
        else:
            print("Student is not part of the record. ")
        break



elif option == 5: #Deletes Record
    record.clear()
    print("All student records are now deleted")



elif option == 0: #Exit
    print("Thank you. Have a nice day!")

elif option ==6: 
    load()

elif option == 7: #Save
    save()

【问题讨论】:

    标签: python string list file dictionary


    【解决方案1】:

    我认为问题在于变量“i”被声明为值为 0,但未在您的“for”语句中更新。 'i' 需要在您遍历记录时引用一个对象。

    【讨论】:

    • 我应该如何解决这个问题?我会在 for 循环下声明“i”吗?
    • 抱歉,'i' 确实更新正确,我的 repl 输入错误。 load 函数以读取模式打开文件,因此您无法更新它。将 'r' 更改为 'w' 将以写入模式打开它,这应该允许您附加记录。
    猜你喜欢
    • 2013-07-12
    • 2016-12-30
    • 2012-09-26
    • 2021-11-26
    • 2018-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-06
    相关资源
    最近更新 更多