others - 保存列表数据

当我关闭一个python文件时,如何保存我添加到列表中的内容。例如,在下面的"my contact"程序中,如果我添加了\'jane\'的信息,那么下次就打开相同的文件。


def main():


 myBook = Book([{"name": \'John Doe\',"phone": \'123-456-7890\',"address": \'1000 Constitution Ave\'}])


class Book:


 def __init__(self, peoples):


 self.peoples = peoples


 self.main_menu()


 def main_menu(self):


 print(\'Main Menu\')


 print(\'1. Display Contact Names\')


 print(\'2. Search For Contacts\')


 print(\'3. Edit Contact\')


 print(\'4. New Contact\')


 print(\'5. Remove Contact\')


 print(\'6. Exit\') 


 self.selection = input(\'Enter a # form the menu: \')


 if (self.selection =="1"):


 self.display_names()


 if (self.selection =="2"):


 self.search()


 if (self.selection =="3"):


 self.edit()


 if (self.selection =="4"):


 self.new()


 if (self.selection =="5"):


 self.delete()


 if (self.selection =="6"):


 self.end()


 def display_names(self):


 for people in self.peoples:


 print("Name:" + people["name"]) 


 self.main_menu() 


 def search(self):


 searchname = input(\'What is the name of your contact: \')


 for index in range(len(self.peoples)):


 if (self.peoples[index]["name"] == searchname):


 print("Name:" + self.peoples[index]["name"])


 print("Address:" + self.peoples[index]["address"])


 print("Phone:" + self.peoples[index]["phone"]) 


 self.main_menu() 


 def edit(self):


 searchname = input(\'What is the name of the contact that you want to edit: \')


 for index in range(len(self.peoples)):


 if (self.peoples[index]["name"] == searchname): 


 self.peoples.pop(index)


 name = input(\'What is your name: \')


 address = input(\'What is your address: \')


 phone = input(\'What is your phone number: \')


 self.peoples.append({"name": name,"phone": phone,"address": address})


 self.main_menu()


 def new(self):


 name = input(\'What is your name: \')


 address = input(\'What is your address: \')


 phone = input(\'What is your phone number: \')


 self.peoples.append({"name": name,"phone": phone,"address": address})


 self.main_menu()


 def delete(self):


 searchname = input(\'What is the name of the contact that you want to delete: \')


 for index in reversed(range(len(self.peoples))):


 if (self.peoples[index]["name"] == searchname): 


 self.peoples.pop(index)



 print(searchname, \'has been removed\')


 self.main_menu() 


 def end(self):


 print(\'Thank you for using the contact book, have a nice day\')


 print(\'Copyright Carson147 2019©, All Rights Reserved\') 



main()



时间:

使用标准库的数据持久性部分中的模块,或将它保存为json,或作为CSV文件

你只需将列表转换为函数内的数组即可。


np.save(\'path/to/save\', np.array(your_list))



要加载:


arr=np.load(\'\'path/to/save.npy\').tolist()



序列化有多种方式,但是我最爱是JSON ,JavaScript对象表示法类似于:


[


"this",


"is",


"a",


"list",


"of",


"strings",


"with",


"a",


 {


"dictionary":"of",


"values": 4,


"an":"example"


 },


"can strings be single-quoted?",


 false,


"can objects nest?",


 {


"I": {


"Think": {


"They":"can"


 }


 }


 }


]




>>> import json


>>> data = [\'a\', \'list\', \'full\', \'of\', \'entries\']


>>> json.dumps(data) # dumps will dump to string


["a","list","full","of","entries"]



然后,你可以在程序关闭之前将数据保存到json中,并在启动后从json读取。


# at the top


import json


from pathlib import Path



# at the bottom of your program:


if __name__ == \'__main__\':


 persistence = Path(\'book.json\')


 if persistence.exists():


 with persistence.open() as f:


 data = json.load(f)


 else:


 data = [{"name": \'John Doe\',"phone": \'123-456-7890\',"address": \'1000 Constitution Ave\'}]



 book = Book(data)


 with persistence.open(\'w\') as f:


 json.dump(f, indent=4)



...