Python编程_Lesson014_json和pickle模块讲解
来源:互联网 发布:mars安卓视频教程json 编辑:程序博客网 时间:2024/05/17 23:34
json和pickle模块的使用
整个的文件结构图如下:
poedu_shop.py
import PoeduShop.user_operatoruser_info_fp = "all_user_info.pickle"def main(): username = input("username:") password = input("password:") if PoeduShop.user_operator.user_login(username, password, user_info_fp): print("login success") else: print("login failed!") while True: print(""" Please input: y: register a new user n: do not register """) user_input = input() if user_input == "y": username = input("username:") while username == "": username = input("username:") password = input("password:") while password == "": password = input("password:") salary = input("salary:") while salary == "": salary = input("salary:") balance = input("balance:") while balance == "": balance = input("balance:") if PoeduShop.user_operator.user_register(username, password, salary, balance, user_info_fp): print("register success!") break else: print("register failed!") elif user_input == "n": breakif __name__ == "__main__": main()else: print("Please Start The Progress from poedu_shop.py module!")
file_operator.py
import os.pathdef __get_file_suffix(fp): """ __get_file_suffix(fp) Get file suffix :param fp: file path :return: return file suffix """ return os.path.splitext(fp)[1]def __get_module_info(fp): """ __get_module_info(fp) Get file format :param fp: file path :return: if the suffix either equals ".json" or equals ".pickle" return module info which includes module name, open and write mode, else return None """ module_info = None suffix = __get_file_suffix(fp) if suffix == ".json": module_info = ["json", "r", "w"] elif suffix == ".pickle": module_info = ["pickle", "rb", "wb"] return module_infodef dump_file(list_data, fp): """ dump_file(dict_data, fp) dump the dict_data to file with fp :param list_data: the data storage in the form of list :param fp: file path :return: if storage success return True, else return False """ return_value = True module_info = __get_module_info(fp) if module_info is None: return_value = False print("An unsupported file format") else: current_module = __import__(module_info[0]) with open(fp, module_info[2]) as file: current_module.dump(list_data, file) return return_valuedef load_file(fp): user_info_dict = None module_info = __get_module_info(fp) if module_info is None: print("An unsupported file format") else: current_module = __import__(module_info[0]) try: with open(fp, module_info[1]) as file: user_info_dict = current_module.load(file) except Exception as e: print(e) return user_info_dict
user_operator.py
import PoeduShop.file_operatorall_user_info_list = []def user_register(username, password, salary, balance, fp): """ user_register(username, password, salary, balance) register a new user with user name, password, monthly salary and user balance. :param username: User Name :param password: Pass Word :param salary: Monthly Salary :param balance: User Balance :param fp: file path :return: return True if register success, else return False! """ all_user_info_list.append({"username": username, "password": password, "salary": salary, "balance": balance}) return PoeduShop.file_operator.dump_file(all_user_info_list, fp)def user_login(username, password, fp): login_status = False user_info_list = PoeduShop.file_operator.load_file(fp) if user_info_list is not None: all_user_info_list.clear() for user_info in user_info_list: all_user_info_list.append(user_info) if username == user_info["username"] and password == user_info["password"]: login_status = True break return login_status
阅读全文
1 0
- Python编程_Lesson014_json和pickle模块讲解
- python核心模块之pickle和cPickle讲解
- python 核心模块之pickle和cPickle讲解
- python核心模块之pickle和cPickle讲解
- python核心模块之pickle和cPickle讲解
- Python pickle模块和joblib模块浅析
- python模块 - pickle模块
- python核心模块pickle和cPickle
- python核心模块pickle和cPickle
- python核心模块pickle和cPickle
- python核心模块之pickle和cPickle
- python序列化模块json和pickle
- python持久化模块:pickle和Cpickle
- Python数据存储:pickle模块的使用讲解
- python-pickle模块
- Python pickle模块学习
- pickle模块, python
- 【Python】pickle模块
- 最短路的神奇用法 bzoj1550 避开怪兽
- HealthInfo.java11
- IO流的简单基本介绍
- centos7操作记录
- HDU
- Python编程_Lesson014_json和pickle模块讲解
- python爬虫(1) 之下载图片
- php遍历多维数组函数
- Spring Boot下使用JPA报错:'hibernate.dialect' not set的解决办法
- C#总结(三)
- Poj-1016 青蛙的约会(扩展欧几里德)
- IgniteRDD学习笔记(五)缓存模式CacheMode
- MyBaseAdapter.java11
- /*阿帕奇安装&开启阿帕奇服务(挂第一张盘)*/