####列表序列化 #Author:walter #json和pickle 的用法相同,单如果涉及到函数序列化要用pickle #序列化一次,反序列化一次。pickle打开需要二进制b import json, pickle #操作 # def sayhi(name): # print("hello,",name) info = { 'name':'lzq', 'age':28, #'func':sayhi } f = open("test.text","w") f.write( json.dumps(info)) print(json.dumps(info)) #f.write(str(info)) #data = pickle.loads(f) ### ==data = pickle.loads(f.read()) #print(data["func"]) f.close() #########反序列化
#Author:walter import json f = open("test.text","r") data = json.loads(f.read()) print(data["age"]) ##########方法一 # data= eval(f.read()) # f.close() # print(data['age']) ########
###调用不同目录下的文件函数等
#Author:walter import os import sys BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) sys.path.append(BASE_DIR) #添加到系统环境变量 from conf import settings from core import main main.login() ######