简体   繁体   English

如何防止我的 JSON 文件被 Python 覆盖?

[英]How can I prevent my JSON file from getting overwritten in Python?

I am currently building a school homework project - a contact manager, using classes in Python.我目前正在构建一个学校作业项目 - 一个联系人经理,使用 Python 中的课程。

I was able to assemble all the needed parts to make my code work, yet upon exiting the program with Terminal, the data inside of my JSON file keep being overwritten.我能够组装所有需要的部分以使我的代码工作,但是在使用终端退出程序后,我的 JSON 文件中的数据不断被覆盖。

I've tried changing the with open attribute to "w" and "a", sadly nothing works.我尝试将 with open 属性更改为“w”和“a”,遗憾的是没有任何效果。

Main Code:主要代码:

from colorama import Fore, Style
from LoadAndSave import load_file, save_file
from ContactBook import ContactBook

contacts = []
DATA_FILE = "contactbook.json"

def menu():
    print(Fore.RED + "Welcome to Contacts Manager v2.0.0!" + Style.RESET_ALL)
    print(Fore.LIGHTCYAN_EX + "Please enter your selection according to the menu." + Style.RESET_ALL)
    print("a - Add a contact")
    print("r - Remove a contact")
    print("s - Search for a contact")
    print("p - Print a list of all contacts")
    print("x - Exit")
    user_selection = input(Fore.GREEN + "Your selection: " + Style.RESET_ALL)
    return user_selection

def main():
    load_file(DATA_FILE)
    contactbook = ContactBook()
    user_selection = ""
    while user_selection != "x":
        user_selection = menu() # As long as user did not select "x", program will always run. 
        if user_selection == "a":
            contactbook.add_contact(input("Please enter contact's name: "), int(input("Please enter contact's number: ")))
        if user_selection == "r":
            contactbook.remove_contact(contactbook.search_contact(input("Contact's Name? ")))
        if user_selection == "s": 
            contactbook.search_contact(input("Contact's name?"))
            print(Fore.GREEN + "Success!" + Style.RESET_ALL)
        if user_selection == "p":
            print(contactbook)
            print(Fore.GREEN + "Success!" + Style.RESET_ALL)
    print(Fore.MAGENTA + "Thank you for using my software. Bye!")
    save_file(contactbook.make_contact_json(), DATA_FILE)
            

if __name__ == "__main__":
    main()

Load and save:加载和保存:

import json

def load_file(DATA_FILE): # Loading JSON file with all information
    with open (DATA_FILE) as file:
        return json.load(file)

def save_file(json_list, DATA_FILE): 
    with open (DATA_FILE, "w") as file: 
        json.dump(json_list, file, indent = 4)

Contact & ContactBook classes:联系方式和联系方式类:

import json

class Contact:
    name = ""
    tel = 0

    def __init__(self, name = "", tel = 0):
        self.name = name
        self.tel = tel

    def __str__(self):
        return json.dumps({"contact_info":[{"name": self.name, "tel": self.tel}]})
from colorama import Fore, Style, Back
from Contact import Contact
import json

class ContactBook:
    contacts = []

    def __init__(self):
        pass

    def add_contact(self, name = "", tel = 0):
        self.contacts.append(Contact(name, tel))
        print(Fore.GREEN + "Success!" + Style.RESET_ALL)

    def remove_contact(self, contact_name):
        self.contacts.remove(contact_name)
        print(Fore.RED + "Removed.\n" + Style.RESET_ALL)

    def search_contact(self, contact_name):
        for contact in self.contacts:
            if type(contact) is Contact:
                if contact.name == contact_name:
                    return contact
        return (Back.RED + "\nThere is no contact with that name." + Style.RESET_ALL + "\n")
    
    def make_contact_json(self):
        result = []
        for contact in self.contacts:
            result.append(json.loads(contact.__str__()))
        return result

    def __str__(self) -> str:
        result = ""
        for contact in self.contacts:
            result += contact.__str__()
        return result

JSON File: JSON 文件:

[
    [
        {
            "name": "sdfgh",
            "tel": 654321
        }
    ]
]

Thanks in advance to all helpers!在此先感谢所有帮助者!

Your contacts are getting overwritten because every time you run your program, you are creating a fresh ContactBook您的联系人正在被覆盖,因为每次运行程序时,您都在创建一个新的 ContactBook

contactbook = ContactBook() # In your Main Code

This in turn initializes an empty contacts list这反过来会初始化一个空的联系人列表

contacts = [] # Inside Class ContactBook

I suggest that you add an __init__ logic to you ContactBook Class that initialises your contacts list with the existing ones我建议您向 ContactBook Class 添加一个__init__逻辑,以使用现有联系人列表初始化您的联系人列表

Something Like就像是

class ContactBook:

   def __init__(self, existing_contacts=None):
      self.contacts = [] if existing_contacts is None else existing_contacts

Also while creating the ContactBook object in you main code please pass the existing contacts.此外,在您的主代码中创建ContactBook object 时,请传递现有联系人。

try:
   existing_contacts = load_file(DATA_FILE)
except FileNotFoundError:
   existing_contacts = None
contactbook = ContactBook(existing_contacts)

Also please make sure that you are saving the contacts correctly.另外请确保您正确保存联系人。 It seems to currently be a list of list of objects where as it should ideally be a list of objects它目前似乎是一个对象列表,理想情况下它应该是一个对象列表

Hope you get good grades.希望你考出好成绩。 Peace out.平安出来。

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

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