今日内容主要有:

一、pickle模块
二、shelve模块
三、json模块
四、json练习
五、xml模块
六、xml练习
七、configparser模块

一、pickle模块

#pickle是一个用来序列化的模块
#序列化是什么?
#指的是将内存中的数据结构转换为一种中间格式 并存储到硬盘上#反序列化是什么?
#将硬盘上存储的中间格式数据再还原为内存中的数据结构#为什么要序列化?
#就是为了将数据持久存储
#之前学过的文件也能持久化存储 但是操作起来非常麻烦
#总结:pickle模块主要功能
#    dump
#   load
#    dumps 序列化
#    loads  序列化
#   不带s是帮你封装了read和writhimport pickle#用户注册得到的数据
name = "高根"
password = "123"
height = "1.5"
hobby = ["吃","喝","飘"]with open("userdb.txt","wt",encoding="utf-8") as f:text="|".join([name,password,str(height)])f.write(text)#序列化的过程
user={"name":name,"password":password,"height":height,"hobby":hobby}#支持python中所有格式
with open("userdb.pkl","wb",)as f:userbytes=pickle.dumps(user)f.write(userbytes)#反序列化过程
with open("userdb.pkl","rb")as f:userbytes=f.read()user=pickle.loads(userbytes)print(user)print(type(user))#dump使用 直接序列化到文件with open("userdb.pkl","wb")as f:pickle.dump(user,f)#load从文件反序列化
with open("userdb.pkl","rb")as f:user=pickle.load(f)print(user)#打包成函数(序列化)
def myload(filename):with open(filename,"rb") as f:return pickle.load(f)

二、shelve模块

#内存->内置->sys.path
import shelve"""shelve模块也用于序列化它与pickle不同之处在于 不需要关心文件模式什么的 直接把他当成一个字典来看待它可用于进行修改
"""
user={"name":"高根"}
s = shelve.open("userdb.shv")
s["user"]=user
s.close()s=shelve.open("userdb.shv",writeback=True)
print(s["user"])
s["user"]["age"] = 20
s.close()

三、json模块

"""pickle和shelve 序列化后的到的数据只能python才能解析通常企业开发不可能做一个当即程序 都需要联网进行计算机间交互我么必须要保证这个数据 能够跨平台使用JSON是什么? Java script object notation 就是对象表示法var obj = {“name”:“egon”}对于我们开发而言,json就是一种通用的数据格式 任何语言都能解析js中的数据类型  python数据类型的对应关系{}              字典[]              liststring“”        strint/float       int/floattrue/false      True/Falsenull            Nonejson的语法规范最外层通常是一个字典或列表{} or []只要你想写个json格式的数据最外层直接写{}字符串必须是双引号,可以在面套任意多的层次json模块的核心功能dumpdumpsloadloads不带s 封装了write和read功能"""import json# 反序列化
with open("a.json","rt",encoding="utf-8")as f:res=json.loads(f.read())print(type(res))with open("a.json",encoding="utf-8") as f:print(json.load(f))# 直接解析字符串的json为python对象

jsontext = """{"user": [{"name": "egon","age": "18"},{"name": "egon","age": "18"}]
}"""res=json.loads(jsontext)
print(res)mydic={"user": [{"name": "egon","age": "18"},{"name": "egon","age": "18"}]
}with open("b.json", "wt",encoding="utf-8") as f:f.write(json.dumps(mydic))with open("b.json", "wt",encoding="utf-8") as f:json.dump(mydic, f)dic = {"a":"理查德姑妈","b":"杰克船长","c":"神秘客人"}
with open("c.json","wt",encoding="utf-8") as f:f.write(json.dumps(dic))with open("c.json","rt",encoding="utf-8")as f:d = json.loads(f.read())print(d)

四、json练习

#将下面内容存储到(news.json)文件

"reason":"成功的返回","result":{"stat":"1","data":[{"uniquekey":"35efd2ba169d7f965669d44eb7cb8a2e","title":"原油技术分析:API意外减少油价暂获支撑 晚间EIA将决定油价命运","date":"2018-10-17 10:15","category":"头条","author_name":"FX168财经","url":"http:\/\/mini.eastday.com\/mobile\/181017101548328.html","thumbnail_pic_s":"http:\/\/05imgmini.eastday.com\/mobile\/20181017\/20181017101548_ce78d3f30d852abd47767db8f203f2fc_1_mwpm_03200403.jpg"},{"uniquekey":"1618e0662b838a153513c5b2a052e25f","title":"战胜荷兰后,中国女排已提前完成目标,郎平27个字霸气宣战意大利","date":"2018-10-17 10:08","category":"头条","author_name":"蓝天侃球","url":"http:\/\/mini.eastday.com\/mobile\/181017100831808.html","thumbnail_pic_s":"http:\/\/06imgmini.eastday.com\/mobile\/20181017\/20181017100831_0b2d9fb72339fc3cc95650ff8bfa133b_1_mwpm_03200403.jpg","thumbnail_pic_s02":"http:\/\/06imgmini.eastday.com\/mobile\/20181017\/20181017100831_0b2d9fb72339fc3cc95650ff8bfa133b_2_mwpm_03200403.jpg","thumbnail_pic_s03":"http:\/\/06imgmini.eastday.com\/mobile\/20181017\/20181017100831_0b2d9fb72339fc3cc95650ff8bfa133b_3_mwpm_03200403.jpg"},{"uniquekey":"64a242ea9d12444cc36b2343810f71d9","title":"华为何刚:华为Mate20系列国内版比海外版配置更高!","date":"2018-10-17 10:03","category":"头条","author_name":"锋科技","url":"http:\/\/mini.eastday.com\/mobile\/181017100355012.html","thumbnail_pic_s":"http:\/\/07imgmini.eastday.com\/mobile\/20181017\/20181017100355_b993a230dffab4a79c14faf62c4b69b3_1_mwpm_03200403.jpg","thumbnail_pic_s02":"http:\/\/07imgmini.eastday.com\/mobile\/20181017\/20181017100355_b993a230dffab4a79c14faf62c4b69b3_2_mwpm_03200403.jpg","thumbnail_pic_s03":"http:\/\/07imgmini.eastday.com\/mobile\/20181017\/20181017100355_b993a230dffab4a79c14faf62c4b69b3_3_mwpm_03200403.jpg"},{"uniquekey":"0e81695aa774146ae79eef96ef41f476","title":"盘点NBA4大“绝世好男人”,威少为妻不逛夜店,库里初中定情","date":"2018-10-17 10:02","category":"头条","author_name":"体坛之王","url":"http:\/\/mini.eastday.com\/mobile\/181017100227516.html","thumbnail_pic_s":"http:\/\/06imgmini.eastday.com\/mobile\/20181017\/20181017100227_ff4ea97e77601d281e804115b692d72f_3_mwpm_03200403.jpg","thumbnail_pic_s02":"http:\/\/06imgmini.eastday.com\/mobile\/20181017\/20181017100227_ff4ea97e77601d281e804115b692d72f_4_mwpm_03200403.jpg","thumbnail_pic_s03":"http:\/\/06imgmini.eastday.com\/mobile\/20181017\/20181017100227_ff4ea97e77601d281e804115b692d72f_2_mwpm_03200403.jpg"},{"uniquekey":"c69cd1bc7ce70e3f100fa0ffa0878776","title":"一个番茄5味药!男人强身,女人养颜,老人抗癌,错过真亏","date":"2018-10-17 10:01","category":"头条","author_name":"39健康网","url":"http:\/\/mini.eastday.com\/mobile\/181017100120544.html","thumbnail_pic_s":"http:\/\/02imgmini.eastday.com\/mobile\/20181017\/20181017_cdc69488259c28d2b5c222d176f22cbd_cover_mwpm_03200403.jpg","thumbnail_pic_s02":"http:\/\/02imgmini.eastday.com\/mobile\/20181017\/20181017_1f86c1e51920783971af2366c00188af_cover_mwpm_03200403.jpg","thumbnail_pic_s03":"http:\/\/02imgmini.eastday.com\/mobile\/20181017\/20181017_e7358d8849b746133be79b2d1d7c0f31_cover_mwpm_03200403.jpg"},{"uniquekey":"6ebf32c2a30704d0503910114ba396af","title":"创业板估值位于历史最低 但业绩增速放缓分析师继续唱空","date":"2018-10-17 10:00","category":"头条","author_name":"财联社","url":"http:\/\/mini.eastday.com\/mobile\/181017100055547.html","thumbnail_pic_s":"http:\/\/04imgmini.eastday.com\/mobile\/20181017\/20181017100055_82eff7d2425f422b3cc0ad37d1982255_3_mwpm_03200403.jpg","thumbnail_pic_s02":"http:\/\/04imgmini.eastday.com\/mobile\/20181017\/20181017100055_82eff7d2425f422b3cc0ad37d1982255_1_mwpm_03200403.jpg"},{"uniquekey":"a7e459aebf8aa6d74e7ddc4b156baf36","title":"2018赛季奖励一览 S8赛季胜利女神皮肤奖励介绍","date":"2018-10-17 10:00","category":"头条","author_name":"斗玩网","url":"http:\/\/mini.eastday.com\/mobile\/181017100045828.html","thumbnail_pic_s":"http:\/\/03imgmini.eastday.com\/mobile\/20181017\/20181017100045_99ac69a133f6b87f242b3651cb747b83_6_mwpm_03200403.jpg","thumbnail_pic_s02":"http:\/\/03imgmini.eastday.com\/mobile\/20181017\/20181017100045_99ac69a133f6b87f242b3651cb747b83_2_mwpm_03200403.jpg","thumbnail_pic_s03":"http:\/\/03imgmini.eastday.com\/mobile\/20181017\/20181017100045_99ac69a133f6b87f242b3651cb747b83_1_mwpm_03200403.jpg"},{"uniquekey":"c4d04bf47c5a45bcb7f0313b60de84d8","title":"国产全新大众速腾比朗逸大一圈!这回你满意了吧!","date":"2018-10-17 10:00","category":"头条","author_name":"快车报","url":"http:\/\/mini.eastday.com\/mobile\/181017100014570.html","thumbnail_pic_s":"http:\/\/03imgmini.eastday.com\/mobile\/20181017\/20181017100014_316d207ba4061e7d5651db8259e88319_8_mwpm_03200403.jpg","thumbnail_pic_s02":"http:\/\/03imgmini.eastday.com\/mobile\/20181017\/20181017100014_316d207ba4061e7d5651db8259e88319_3_mwpm_03200403.jpg","thumbnail_pic_s03":"http:\/\/03imgmini.eastday.com\/mobile\/20181017\/20181017100014_316d207ba4061e7d5651db8259e88319_11_mwpm_03200403.jpg"},{"uniquekey":"48721b05ac0e8cc8a1419f4dde398984","title":"终于看到NPC团综的希望,九个小哥哥集体飞北京,疑似团综开拍","date":"2018-10-17 10:00","category":"头条","author_name":"用微笑离开后的坚强","url":"http:\/\/mini.eastday.com\/mobile\/181017100006065.html","thumbnail_pic_s":"http:\/\/03imgmini.eastday.com\/mobile\/20181017\/20181017100006_29e6023f4ca7a46c5eb7f470ac2c9eb3_3_mwpm_03200403.jpg","thumbnail_pic_s02":"http:\/\/03imgmini.eastday.com\/mobile\/20181017\/20181017100006_29e6023f4ca7a46c5eb7f470ac2c9eb3_1_mwpm_03200403.jpg","thumbnail_pic_s03":"http:\/\/03imgmini.eastday.com\/mobile\/20181017\/20181017100006_29e6023f4ca7a46c5eb7f470ac2c9eb3_6_mwpm_03200403.jpg"},{"uniquekey":"2bdc6124ecee5a3789c994dfec2ced06","title":"RNG小组第一EDG小组第二 这让网友产生一个可怕的想法!","date":"2018-10-17 09:58","category":"头条","author_name":"淘游助手","url":"http:\/\/mini.eastday.com\/mobile\/181017095804681.html","thumbnail_pic_s":"http:\/\/05imgmini.eastday.com\/mobile\/20181017\/20181017095804_a2c4f5c0b9035a369f10027a834c9fe9_3_mwpm_03200403.jpg","thumbnail_pic_s02":"http:\/\/05imgmini.eastday.com\/mobile\/20181017\/20181017095804_a2c4f5c0b9035a369f10027a834c9fe9_1_mwpm_03200403.jpg","thumbnail_pic_s03":"http:\/\/05imgmini.eastday.com\/mobile\/20181017\/20181017095804_a2c4f5c0b9035a369f10027a834c9fe9_4_mwpm_03200403.jpg"},{"uniquekey":"21b311386ac530f1a2dffbfe1f3f6f73","title":"电动自行车行业发展现状分析 市场或将迎来洗牌","date":"2018-10-17 09:57","category":"头条","author_name":"前瞻产业研究院","url":"http:\/\/mini.eastday.com\/mobile\/181017095726864.html","thumbnail_pic_s":"http:\/\/04imgmini.eastday.com\/mobile\/20181017\/20181017095726_e39f7c4dcf076c5ae666c6fe6771d562_1_mwpm_03200403.jpg","thumbnail_pic_s02":"http:\/\/04imgmini.eastday.com\/mobile\/20181017\/20181017095726_e39f7c4dcf076c5ae666c6fe6771d562_2_mwpm_03200403.jpg"},{"uniquekey":"b34556aeb06e8f2bef654bee4e428137","title":"半决赛中国VS意大利,意大利输球挑中国队,朗平:他们会后悔","date":"2018-10-17 09:56","category":"头条","author_name":"学尔纳","url":"http:\/\/mini.eastday.com\/mobile\/181017095644664.html","thumbnail_pic_s":"http:\/\/02imgmini.eastday.com\/mobile\/20181017\/20181017095644_ae19fb27fbaa7a3a197181e9eae34452_2_mwpm_03200403.jpg","thumbnail_pic_s02":"http:\/\/02imgmini.eastday.com\/mobile\/20181017\/20181017095644_ae19fb27fbaa7a3a197181e9eae34452_3_mwpm_03200403.jpg","thumbnail_pic_s03":"http:\/\/02imgmini.eastday.com\/mobile\/20181017\/20181017095644_ae19fb27fbaa7a3a197181e9eae34452_1_mwpm_03200403.jpg"},{"uniquekey":"fee031516e7f7f78c48a6644a7cb36d2","title":"郎平再造霸气金句!央视记者无脑提问再被一句话高智商化解","date":"2018-10-17 09:55","category":"头条","author_name":"peace说球","url":"http:\/\/mini.eastday.com\/mobile\/181017095533594.html","thumbnail_pic_s":"http:\/\/09imgmini.eastday.com\/mobile\/20181017\/20181017095533_d41d8cd98f00b204e9800998ecf8427e_3_mwpm_03200403.jpg","thumbnail_pic_s02":"http:\/\/09imgmini.eastday.com\/mobile\/20181017\/20181017095533_d41d8cd98f00b204e9800998ecf8427e_4_mwpm_03200403.jpg","thumbnail_pic_s03":"http:\/\/09imgmini.eastday.com\/mobile\/20181017\/20181017095533_d41d8cd98f00b204e9800998ecf8427e_1_mwpm_03200403.jpg"},{"uniquekey":"1abc34144298526928dfb5f828d0bfe6","title":"郭靖为国镇守襄阳,那俏黄蓉私生活是如何过的小说已给出答案!","date":"2018-10-17 09:53","category":"头条","author_name":"北岛的旧梦","url":"http:\/\/mini.eastday.com\/mobile\/181017095319608.html","thumbnail_pic_s":"http:\/\/03imgmini.eastday.com\/mobile\/20181017\/20181017095319_eb067917571ed207545690a41607d494_3_mwpm_03200403.jpg","thumbnail_pic_s02":"http:\/\/03imgmini.eastday.com\/mobile\/20181017\/20181017095319_eb067917571ed207545690a41607d494_4_mwpm_03200403.jpg","thumbnail_pic_s03":"http:\/\/03imgmini.eastday.com\/mobile\/20181017\/20181017095319_eb067917571ed207545690a41607d494_2_mwpm_03200403.jpg"},{"uniquekey":"4e9539aa0cdfb7e5e3fbb4748aaee9ed","title":"恭喜3生肖!未来7天,横财一马当先,有钱有地位,财富再上新台阶","date":"2018-10-17 09:53","category":"头条","author_name":"阿木细说","url":"http:\/\/mini.eastday.com\/mobile\/181017095300162.html","thumbnail_pic_s":"http:\/\/08imgmini.eastday.com\/mobile\/20181017\/20181017095300_d41d8cd98f00b204e9800998ecf8427e_3_mwpm_03200403.jpg","thumbnail_pic_s02":"http:\/\/08imgmini.eastday.com\/mobile\/20181017\/20181017095300_d41d8cd98f00b204e9800998ecf8427e_1_mwpm_03200403.jpg","thumbnail_pic_s03":"http:\/\/08imgmini.eastday.com\/mobile\/20181017\/20181017095300_d41d8cd98f00b204e9800998ecf8427e_2_mwpm_03200403.jpg"},{"uniquekey":"99b4ca1dbada3ad4b9c7bb94f56fdf86","title":"曝巴萨大将赴上海看大师赛引俱乐部不满 别把心思全花在网球上","date":"2018-10-17 09:52","category":"头条","author_name":"足球部落国际版","url":"http:\/\/mini.eastday.com\/mobile\/181017095221673.html","thumbnail_pic_s":"http:\/\/04imgmini.eastday.com\/mobile\/20181017\/20181017095221_22a8dc9540f3f635aab47c781395d454_1_mwpm_03200403.jpg"},{"uniquekey":"a4b3e2885c14f59ac99c1ffa7c8af470","title":"大型魔术秀:消失的身体!求求你们放过近视眼吧……哈哈","date":"2018-10-17 09:49","category":"头条","author_name":"HOT男人","url":"http:\/\/mini.eastday.com\/mobile\/181017094950231.html","thumbnail_pic_s":"http:\/\/03imgmini.eastday.com\/mobile\/20181017\/20181017094950_3862216a8fc853bd21921b0891df563d_2_mwpm_03200403.jpg","thumbnail_pic_s02":"http:\/\/03imgmini.eastday.com\/mobile\/20181017\/20181017094950_3862216a8fc853bd21921b0891df563d_5_mwpm_03200403.jpg","thumbnail_pic_s03":"http:\/\/03imgmini.eastday.com\/mobile\/20181017\/20181017094950_3862216a8fc853bd21921b0891df563d_15_mwpm_03200403.jpg"},{"uniquekey":"8f4d38978ccf590a6b99aabf195f2652","title":"夜猫子的你应该知道的“小秘密”","date":"2018-10-17 09:48","category":"头条","author_name":"广州建国医院精神科","url":"http:\/\/mini.eastday.com\/mobile\/181017094842986.html","thumbnail_pic_s":"http:\/\/01imgmini.eastday.com\/mobile\/20181017\/20181017_0a964b132e85c005a82ac71c2ab645af_mwpm_03200403.jpg"},{"uniquekey":"ffc9347ca9a76a60ca4e042a66cd6e0a","title":"不卸妆就分手?19岁男生为爱卸掉自己挚爱的妆容,素颜让人震撼","date":"2018-10-17 09:47","category":"头条","author_name":"派大星的小可爱A","url":"http:\/\/mini.eastday.com\/mobile\/181017094719931.html","thumbnail_pic_s":"http:\/\/04imgmini.eastday.com\/mobile\/20181017\/20181017094719_3fd44b93a4fce69dfcc2178d41bebe4f_3_mwpm_03200403.jpg","thumbnail_pic_s02":"http:\/\/04imgmini.eastday.com\/mobile\/20181017\/20181017094719_3fd44b93a4fce69dfcc2178d41bebe4f_2_mwpm_03200403.jpg","thumbnail_pic_s03":"http:\/\/04imgmini.eastday.com\/mobile\/20181017\/20181017094719_3fd44b93a4fce69dfcc2178d41bebe4f_4_mwpm_03200403.jpg"},{"uniquekey":"19337c495cd9c24627f2585401b356a1","title":"52 家房企六成三季报预增 楼市降温房企遇去化难题","date":"2018-10-17 09:45","category":"头条","author_name":"搜狐焦点","url":"http:\/\/mini.eastday.com\/mobile\/181017094523953.html","thumbnail_pic_s":"http:\/\/08imgmini.eastday.com\/mobile\/20181017\/20181017094523_1534fba9aefb0e0ca3759f0a38d83bf9_1_mwpm_03200403.jpg"},{"uniquekey":"0870f47f7c2f7c91bc41343585b60e2d","title":"成都地铁2号线女子持凶器伤人致2人划伤 均无生命危险","date":"2018-10-17 09:44","category":"头条","author_name":"中国新闻网四川新闻","url":"http:\/\/mini.eastday.com\/mobile\/181017094450133.html","thumbnail_pic_s":"http:\/\/05imgmini.eastday.com\/mobile\/20181017\/20181017094450_fabd1fc33a4e0327401f5280e5d814d5_1_mwpm_03200403.jpg"},{"uniquekey":"e1bc4da7568ecb563c6a9addb6a35d34","title":"浪琴的30款时尚名表,最爱哪一款?","date":"2018-10-17 09:44","category":"头条","author_name":"灌云醉意浓服装","url":"http:\/\/mini.eastday.com\/mobile\/181017094431457.html","thumbnail_pic_s":"http:\/\/07imgmini.eastday.com\/mobile\/20181017\/20181017094431_7cb7f11cf40d2a7d7add376dae350545_24_mwpm_03200403.jpg","thumbnail_pic_s02":"http:\/\/07imgmini.eastday.com\/mobile\/20181017\/20181017094431_7cb7f11cf40d2a7d7add376dae350545_25_mwpm_03200403.jpg","thumbnail_pic_s03":"http:\/\/07imgmini.eastday.com\/mobile\/20181017\/20181017094431_7cb7f11cf40d2a7d7add376dae350545_20_mwpm_03200403.jpg"},{"uniquekey":"e21bf3ecf60ede138f7f858273708812","title":"邻居嘲笑我新家装修成现代风,说我不懂装修,装修好才打他们的脸","date":"2018-10-17 09:40","category":"头条","author_name":"家居小吴","url":"http:\/\/mini.eastday.com\/mobile\/181017094027402.html","thumbnail_pic_s":"http:\/\/09imgmini.eastday.com\/mobile\/20181017\/20181017_5cc7cc189378d7cdfcb04deeb86aea2d_cover_mwpm_03200403.jpg","thumbnail_pic_s02":"http:\/\/09imgmini.eastday.com\/mobile\/20181017\/20181017_3a8819d45cc18f7b3b10e8b0e34edba4_cover_mwpm_03200403.jpg","thumbnail_pic_s03":"http:\/\/09imgmini.eastday.com\/mobile\/20181017\/20181017_0bcd2ba151bc0e88abdf6d67e5da76ee_cover_mwpm_03200403.jpg"},{"uniquekey":"ff227178528e45dba654d7a4f01c1151","title":"搞笑GIF: 这才叫真正的绝望,感觉好委屈","date":"2018-10-17 09:37","category":"头条","author_name":"快乐分享","url":"http:\/\/mini.eastday.com\/mobile\/181017093735819.html","thumbnail_pic_s":"http:\/\/06imgmini.eastday.com\/mobile\/20181017\/20181017_afdeb47ff89c02e1174f60040c81f660_cover_mwpm_03200403.jpg","thumbnail_pic_s02":"http:\/\/06imgmini.eastday.com\/mobile\/20181017\/20181017_49f1a3d7e7df7b70726a5a04528f300e_cover_mwpm_03200403.jpg","thumbnail_pic_s03":"http:\/\/06imgmini.eastday.com\/mobile\/20181017\/20181017_5839366136736e5e59e66095ed6d8509_cover_mwpm_03200403.jpg"},{"uniquekey":"58fa8c9b43cae11b7406188c4f210736","title":"封面人物|陈丽媛与马拉松的故事","date":"2018-10-17 09:36","category":"头条","author_name":"创客儿","url":"http:\/\/mini.eastday.com\/mobile\/181017093650361.html","thumbnail_pic_s":"http:\/\/01imgmini.eastday.com\/mobile\/20181017\/20181017_91bba059bc1f13045d4ee7272b2f89f6_cover_mwpm_03200403.jpg","thumbnail_pic_s02":"http:\/\/01imgmini.eastday.com\/mobile\/20181017\/20181017_27e61bd909cf4f0b70f9e6621932105b_cover_mwpm_03200403.jpg","thumbnail_pic_s03":"http:\/\/01imgmini.eastday.com\/mobile\/20181017\/20181017_81509701d435920cad186df8aa146dcf_cover_mwpm_03200403.jpg"},{"uniquekey":"d4273930a4a943b60c4a6ef8f5514e97","title":"求生欲如此强的《奇葩说》第五季,如今连李诞都救不活了?","date":"2018-10-17 09:35","category":"头条","author_name":"娱乐大咖秀","url":"http:\/\/mini.eastday.com\/mobile\/181017093523888.html","thumbnail_pic_s":"http:\/\/06imgmini.eastday.com\/mobile\/20181017\/20181017_407422950d0aa228da607ceae526bc38_cover_mwpm_03200403.jpg","thumbnail_pic_s02":"http:\/\/06imgmini.eastday.com\/mobile\/20181017\/20181017_4ae74313691cb097f94e011b3e1cb6b6_cover_mwpm_03200403.jpg","thumbnail_pic_s03":"http:\/\/06imgmini.eastday.com\/mobile\/20181017\/20181017_b71967c9c2ddad93cc76e008bcf8c012_cover_mwpm_03200403.jpg"},{"uniquekey":"cba32d09b6f2a78690fd93e0f10a5642","title":"16岁儿子因路坑出事故死亡 印度男子见坑就填悼念亡子","date":"2018-10-17 09:33","category":"头条","author_name":"杀生丸","url":"http:\/\/mini.eastday.com\/mobile\/181017093301811.html","thumbnail_pic_s":"http:\/\/02imgmini.eastday.com\/mobile\/20181017\/20181017_d709e090413054b34f1e0e019b39a07c_cover_mwpm_03200403.jpg"},{"uniquekey":"d78556f8af18b468e5adf35a862f357b","title":"周星驰新片《D计划》,2019春节档上映,谁能一战?","date":"2018-10-17 09:30","category":"头条","author_name":"夏木渊","url":"http:\/\/mini.eastday.com\/mobile\/181017093050040.html","thumbnail_pic_s":"http:\/\/08imgmini.eastday.com\/mobile\/20181017\/20181017093050_0344e33a5392131d3c6c247ada310846_4_mwpm_03200403.jpg","thumbnail_pic_s02":"http:\/\/08imgmini.eastday.com\/mobile\/20181017\/20181017093050_0344e33a5392131d3c6c247ada310846_8_mwpm_03200403.jpg","thumbnail_pic_s03":"http:\/\/08imgmini.eastday.com\/mobile\/20181017\/20181017093050_0344e33a5392131d3c6c247ada310846_7_mwpm_03200403.jpg"},{"uniquekey":"1cca176fd6cd3af811bfca5975cc8244","title":"坚瑞沃能前三季预亏294400-294900万","date":"2018-10-17 09:30","category":"头条","author_name":"中国发展网","url":"http:\/\/mini.eastday.com\/mobile\/181017093004941.html","thumbnail_pic_s":"http:\/\/07imgmini.eastday.com\/mobile\/20181017\/20181017093004_976bd1cf9b7790efd96e1ef950234b3e_1_mwpm_03200403.jpg"},{"uniquekey":"c0237379b0dc5907dcaad893a8629ca3","title":"不想吃饭就来吃拌面吧,10种超好吃的拌面做法,学会做给老公吃!","date":"2018-10-17 09:29","category":"头条","author_name":"三叔日食记","url":"http:\/\/mini.eastday.com\/mobile\/181017092958385.html","thumbnail_pic_s":"http:\/\/01imgmini.eastday.com\/mobile\/20181017\/20181017092958_ceea58d525367d04016d3c837495bd64_9_mwpm_03200403.jpg","thumbnail_pic_s02":"http:\/\/01imgmini.eastday.com\/mobile\/20181017\/20181017092958_ceea58d525367d04016d3c837495bd64_1_mwpm_03200403.jpg","thumbnail_pic_s03":"http:\/\/01imgmini.eastday.com\/mobile\/20181017\/20181017092958_ceea58d525367d04016d3c837495bd64_6_mwpm_03200403.jpg"}]},"error_code":0
}

news.json (源代码)

习题:把news.json中的新闻数据提取出来打印控制台
with open("news.json","rt",encoding="utf-8") as f:res = json.load(f)print(type(res))for dic in res["result"]["data"]:for key in dic:print("%s:%s"%(key,dic[key]))

五、xml模块

"""XML  可扩展的标记语言<> </>也是一种通用的数据格式,也是一种跨平台学习的重点还是语法格式一、任何的起始标签都必须有⼀一个结束标签。<> </>二、可以采用另一种简化语法,可以在一个标签中同时表示起始和结束标签。这种语法是在⼤于符号之前紧跟一个斜线(/),XML解析器会将其翻译成<百度百科词条></百度百科词条>。例例如<百度百科词条/>。三、标签必须按合适的顺序进⾏行行嵌套,所以结束标签必须按镜像顺序匹配起始标签。这好⽐比是将起始和结束标签看作是数学中的左右括号:在没有关闭所有的内部括号之前,是不不能关闭外⾯面的括号的。四、所有的特性都必须有值。五、所有的特性都必须在值的周围加上双引号。一个标签的组成部分<tagname 属性名称=“属性值”>文本内容<></>单标签写法</tagename> 属性名称=“属性值”/>镜像关闭顺序实例:<a><b><c></c></b></a>
案例:
<studentinfo><张三><age>20</age><gender>boy</gender></张三><李四><age>21</age><gender>gril</gender></李四>
</studentinfo>总结:xml也是一种中间格式 也属于序列化之一与json相比较,同样的数据 json会比xml更小, 效率更高xml 需要根据文档结构 手动解析 而json直接对象"""import xml.etree.ElementTree as ElementTree
#解析 d.xml
tree = ElementTree.parse("d.xml")
print(tree)
#获取根标签
rootTree = tree.getroot()
#三种获取数据的方式
#获取所有人的年龄 iter 是用于在全文范围获取标签
#
for item in rootTree.iter("age"):#一个标签三个组成部分print(item.tag)#标签名称print(item.attrib)#标签的属性print(item.text)#文本内容#第二种 从当前标签的子标签重找到一个名称为age标签 如果有多个 找到的是第一个
print(rootTree.find("age").attrib)#第三种 从当前标签的子标签中找到所有名称为age的标签
print(rootTree.findall("age"))# 获取单个属性
stu = rootTree.find("stu")
print(stu.get("age"))
print(stu.get("name"))#删除子标签
rootTree.remove(stu)#添加子标签
#要创建一个子标签出来
newTag = ElementTree.Element("这是新标签",{"一个属性":"值"})
rootTree.append(newTag)#写入文件
tree.write("f.xml",encoding="utf-8")#ElementTree.pasre

六、xml模块的练习

#f.xml文件

<studentinfo><stu name="张三" age="20"><girlfriend name="张三女朋友" age="19"></girlfriend></stu><stu name="李四" age="21"><girlfriend name="李四女朋友" age="19"></girlfriend></stu><这是新标签 一个属性="值" /></studentinfo>

#执行文件

import xml.etree.ElementTree as ElementTree
tree = ElementTree.parse("f.xml")rootTree = tree.getroot()
users=[]
for item in rootTree.iter("stu"):user = item.attribprint(user)gitem = item.find("girlfriend")user["girlfriend"] = gitem.attridusers.append(user)
print(users)

七、configparser模块

"""
    config  parser
    用于解析配置文件的模块
    何为配置文件
    包含配置程序信息的文件就称为配置文件
    什么样的数据应作为配置信息
    需要改  但是不经常改的信息  例如数据文件的路径 DB_PATH
配置文件中 只有两种内容
    一种是section 分区
    一种是option  选项 就是一个key=value形式
我们用的最多的就是get功能 用来从配置文件获取一个配置选项
练习:
    做一个登录 首先查看配置文件 是否又包含 用户名和密码 如果由直接登录 如果没有就进行输入用户名密码登录
    登录成功后 询问是否要保存密码  如果是 写入配置文件
"""
import configparser
# 创建一个解析器
config = configparser.ConfigParser()
# 读取并解析test.cfg
config.read("test.cfg",encoding="utf-8")
# 获取需要的信息
# 获取所有分区
print(config.sections())
# 获取所有选项
print(config.options("user"))
# 获取某个选项的值
print(config.get("path","DB_PATH"))
print(type(config.get("user","age")))
#
# # get返回的都是字符串类型  如果需要转换类型 直接使用get+对应的类型(bool int float)
print(type(config.getint("user","age")))
print(type(config.get("user","age")))# 是否由某个选项
config.has_option()
# 是否由某个分区
config.has_section()

转载于:https://www.cnblogs.com/wuzhengzheng/p/9813134.html

python常用模块:pickle、shelve、json、xml、configparser相关推荐

  1. python常用模块之shelve模块

    python常用模块之shelve模块 shelve模块是一个简单的k,v将内存中的数据通过文件持久化的模块,可以持久化任何pickle可支持的python数据类型 我们在上面讲json.pickle ...

  2. python shelve模块_python常用模块之shelve模块

    python常用模块之shelve模块 shelve模块是一个简单的k,v将内存中的数据通过文件持久化的模块,可以持久化任何pickle可支持的python数据类型 我们在上面讲json.pickle ...

  3. python中常用的序列化模块_第六章 常用模块(5):python常用模块(序列化模块:pickle,json,shelve,xml)...

    6.3.7 序列化模块 (pickle,json,shelve,xml) 文件写入,数据传输时,我们都是以字符串形式写入的(因为字符串可以encode成bytes). 那其他类型(比如字典,列表等)想 ...

  4. 对于python来说、一个模块就是一个文件-python常用模块

    python常用模块 什么是模块? 常见的场景:一个模块就是一个包含了python定义和声明的文件,文件名就是模块名字加上.py的后缀. 但其实import加载的模块分为四个通用类别: 1 使用pyt ...

  5. Python常用模块——目录

    Python常用模块学习 Python模块和包 Python常用模块time & datetime &random 模块 Python常用模块os & sys & sh ...

  6. Python 常用模块大全

    Python 常用模块大全(整理) OS 模块 #os模块就是对操作系统进行操作,使用该模块必须先导入模块: import os #getcwd() 获取当前工作目录(当前工作目录默认都是当前文件所在 ...

  7. python常用模块大全总结-常用python模块

    广告关闭 2017年12月,云+社区对外发布,从最开始的技术博客到现在拥有多个社区产品.未来,我们一起乘风破浪,创造无限可能. python常用模块什么是模块? 常见的场景:一个模块就是一个包含了py ...

  8. Python常用模块集锦

    常用模块主要分为以下几类(缺失的后续再补充): 时间转换 时间计算 序列化和反序列化:json,pickle 编解码:unicode,base64 加解密:md5,sha1,hmac_sha1,aes ...

  9. 实战篇一 python常用模块和库介绍

    # -_-@ coding: utf-8 -_-@ -- Python 常用模块和库介绍 第一部分:json模块介绍 import json 将一个Python数据结构转换为JSON: dict_ = ...

  10. python常用模块-调用系统命令模块(subprocess)

    python常用模块-调用系统命令模块(subprocess) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. subproces基本上就是为了取代os.system和os.spaw ...

最新文章

  1. PCL中点云的超体素(SuperVoxel)
  2. 30个非常有吸引力的黑色网站设计作品
  3. java调用ole ie_ActiveX (.ocx)的写法,及在IE里调用
  4. Postman工具(环境变量与全局变量)
  5. 给 IDEA 换个酷炫的主题吧,这个有点哇塞啊!
  6. 从零开始学习CANoe(二)—— CANdb++ 创建 dbc文件
  7. 谷歌眼镜公开接受预订,全新宣传视频帅爆了_-Chaz-_新浪博客
  8. 查看计算机CPU核心数和线程数
  9. DuKBitmapImages 图片压缩处理技术
  10. 计算机的磁盘管理在哪,磁盘管理器在哪
  11. 如何在VeryCD中下载资源
  12. 计算机电脑为什么0无法输入,电脑浏览器网页无法输入文字该怎么办?
  13. android倒计时动画特效,Android实现答题倒计时效果
  14. recycleview横向展开_android 横向recyclerView 数据居中,从中间往两边展开显示
  15. 电商指标项目-背景及技术选型
  16. python中4j_GitHub - Byron4j/PythonVeterans: Python新手到老鸟的历程;基于Python 3.6
  17. 案例4加减乘除四则运算
  18. loadRunner之Linux环境下安装负载机
  19. Python爬虫项目之NBA球员可视化分析
  20. ConcurrentHashMap学习

热门文章

  1. 河海大学计算机科学考研真题,河海大学计算机与信息学院845电子技术基础历年考研真题汇编.pdf...
  2. c语言通讯录打电话,求c语言编写的通讯录源代码
  3. java获取url的参数page返回数值page计算_Java常用代码汇总,建议背会
  4. 1.1方程求根之二分法
  5. Apache Cassandra和Apache Ignite:分布式数据库的明智之选
  6. Dell做RAID配置图文全教程
  7. 关于ExtJs4的Grid带 查询 参数 分页(baseParams--extraParams)
  8. 树莓派安装ftp服务器
  9. 修改的梅花雪的日期控件。。只是一些小改动
  10. 计算机python语言_计算机语言Python解释器