#!/usr/bin/env python # coding:UTF-8 """ @version: python3.x @author:曹新健 @contact: [email protected] @software: PyCharm @file: 0925ex.py @time: 2018/9/25 22:37 """ import sys #数据区start zhangSan = {"name":"张三","age":18,"pay":200,"job":"学生"} liSi = {"name":"李四","age":28,"pay":20000,"job":"程序员"} LeeMin = {"name":"LeeMin","age":26,"pay":30000,"job":"销售"} person = {} person["zhangSan"] = zhangSan person["liSi"] = liSi person["LeeMin"] = LeeMin #数据区end #标志区 fileName = "people-file" ENDDB = "enddb." ENDREC = "endrec." RECSEP = "=>" #代码区 def storeDbase(db,file): with open(file,"w") as dbFile: sys.stdout = dbFile for key in db: print(key) for (name,value) in db[key].items(): #print(1,file=dbFile) print(name+RECSEP+repr(value)) print(ENDREC) print(ENDDB,) def loadDbse(file): with open(file) as dbFile: db = {} sys.stdin = dbFile key = input() while key != ENDDB: rec = {} field = input() while field != ENDREC: name,value = field.split(RECSEP) rec[name] = eval(value) field = input() db[key] = rec key = input() return db if __name__ == "__main__": #storeDbase(person,fileName) print(loadDbse(fileName))
Python:用sys.stdin和sys.stdout从文件中读写数据
猜你喜欢
转载自blog.csdn.net/caoxinjian423/article/details/82846556
今日推荐
周排行