python常用模块:pickle、shelve、json、xml、configparser
本文目录:
一、pickle模块
二、shelve模块
三、json模块
四、json练习
五、xml模块
六、xml练习
七、configparser模块
一、pickle模块
pickle是一个用来序列化的模块
序列化是什么?
#指的是将内存中的数据结构转换为一种中间格式 并存储到硬盘上 反序列化是什么?
#将硬盘上存储的中间格式数据再还原为内存中的数据结构 为什么要序列化?
就是为了将数据持久存储
之前学过的文件也能持久化存储 但是操作起来非常麻烦
总结:pickle模块主要功能
dump
load
dumps 序列化
loads 序列化
不带s是帮你封装了read和writh
import pickle #用户注册得到的数据
name = "高根"
password = ""
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模块
shelve模块也用于序列化
它与pickle不同之处在于 不需要关心文件模式什么的 直接把他当成一个字典来看待
它可用于进行修改
#内存->内置->sys.path
import shelve """"""
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数据类型的对应关系
{} 字典
[] list
string“” str
int/float int/float
true/false True/False
null None json的语法规范
最外层通常是一个字典或列表
{} or []
只要你想写个json格式的数据最外层直接写{} 字符串必须是双引号,可以在面套任意多的层次 json模块的核心功能
dump
dumps
load
loads
不带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": ""
},
{
"name": "egon",
"age": ""
}
]
} 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":"",
"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="">
<girlfriend name="张三女朋友" age=""></girlfriend>
</stu>
<stu name="李四" age="">
<girlfriend name="李四女朋友" age=""></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.attrib
print(user)
gitem = item.find("girlfriend")
user["girlfriend"] = gitem.attrid
users.append(user)
print(users)
七、configparser模块
config parser
用于解析配置文件的模块
何为配置文件
包含配置程序信息的文件就称为配置文件
什么样的数据应作为配置信息
需要改 但是不经常改的信息 例如数据文件的路径 DB_PATH
一种是section 分区
一种是option 选项 就是一个key=value形式
做一个登录 首先查看配置文件 是否有包含 用户名和密码 如果有直接登录 如果没有就进行输入用户名密码登录
登录成功后 询问是否要保存密码 如果是 写入配置文件
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()
python常用模块:pickle、shelve、json、xml、configparser的更多相关文章
- python常用模块之shelve模块
python常用模块之shelve模块 shelve模块是一个简单的k,v将内存中的数据通过文件持久化的模块,可以持久化任何pickle可支持的python数据类型 我们在上面讲json.pickle ...
- Python常用模块 (2) (loging、configparser、json、pickle、subprocess)
logging 简单应用 将日志打印到屏幕 import logging logging.debug('debug message') logging.info('info message') log ...
- Python序列化模块pickle和json使用和区别
这是用于序列化的两个模块: • json: 用于字符串和python数据类型间进行转换 • pickle: 用于python特有的类型和python的数据类型间进行转换 Json模块提供了四个功能:d ...
- 模块urllib requests json xml configparser 学习笔记
发起http请求 获取返回值 返回值是字符串 第三方模块安装 pip install requests 返回值格式 xml html jaon json 功能 loads 字符串>&g ...
- 8.2、常用模块介绍2:xml,configparser,hashlib
xml: 介绍:包含关于可扩展标记语言xml的函数 使用: python有三种方法解析XML--SAX,DOM,以及ElementTree,由于xml技术落后,所以这里不对sax,dom介绍: xml ...
- Python 常用模块系列学习(3)--configparser module
configpaser 模块----用于生成和修改常见配置文档 1. config 对象的创建: import configparser #导入模块 config = configparser.Con ...
- python 常用模块 time random os模块 sys模块 json & pickle shelve模块 xml模块 configparser hashlib subprocess logging re正则
python 常用模块 time random os模块 sys模块 json & pickle shelve模块 xml模块 configparser hashlib subprocess ...
- python常用模块之json、pickle模块
python常用模块之json.pickle模块 什么是序列化? 序列化就是把内存里的数据类型转换成字符,以便其能存储到硬盘或者通过网络进行传输,因为硬盘或网络传输时只接受bytes. 为什么要序列化 ...
- 常用模块(random,os,json,pickle,shelve)
常用模块(random,os,json,pickle,shelve) random import random print(random.random()) # 0-1之间的小数 print(rand ...
- (转)python常用模块(模块和包的解释,time模块,sys模块,random模块,os模块,json和pickle序列化模块)
阅读目录 1.1.1导入模块 1.1.2__name__ 1.1模块 什么是模块: 在计算机程序的开发过程中,随着程序代码越写越多,在一个文件里代码就会越来越长,越来越不容易维护. 为了编写可维护的代 ...
随机推荐
- docker 导出多个镜像合并成一个tar
导出单个镜像 docker save [images] > [name.tar] 倒出多个镜像合并成一个tar包 docker save [images] [images] > [name ...
- python-Web-django-商城-购物车商品加减
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Unity3D热更新之LuaFramework篇[04]--自定义UI监听方法
时隔一个多月我又回来啦! 坚持真的是很难的一件事,其它事情稍忙,就很容易说服自己把写博客的计划给推迟了. 好在终于克服了自己的惰性,今天又开始了. 本篇继续我的Luaframework学习之路. 一. ...
- 产品之我见(3)-ZEN在产品上的延伸
ZEN 在WIKIPEDIA上的解释是这样的: 禅,禅宗强调严格的自我控制能力,冥想实践,洞察到的事物性质,而这种洞察力在日常生活中的个人表达,尤其是为了别人的利益. 因此,它不再强调对经典和学说的了 ...
- 支付宝网站即时支付开发,MD5加签名规则处理代码展示
一.如果传入进来的Object对象,最后生成制定格式的字符换 text: list拼接成字符串,map中的所有元素,按照“参数=参数值”的模式用“&”字符拼接成字符串,list的元素之间用“| ...
- 结构体structure
结构体是值类型 import Foundation struct TV{ var keyName="a" var keyNumber=9 func getKey()->Int ...
- [AcWing303/304]任务安排2/3
[AcWing303]任务安排2 有 \(N\) 个任务排成一个序列在一台机器上等待执行,它们的顺序不得改变.机器会把这 \(N\) 个任务分成若干批,每一批包含连续的若干个任务.从时刻 \(0\) ...
- Redis(1.6)Redis发布订阅
[0]发布订阅架构图 客户端订阅某个频道,让后有人在频道上发布信息,频道就分发给所有的客户端. 举个例子:就和微信公众号一样,文章作者者把文章发到微信公众号上,微信公众号平台把文章推送到所有的订 ...
- windows ping命令
ping -a 192.168.xxx.xxx 解析计算机NetBios名 ping -n 数字 192.168.xxx.xxx 发送指定数量的echo数据包数,默认是四个 ping -l 192 ...
- 关于KMP中求next数组的思考【转】
文章转自 http://www.tuicool.com/articles/yayeIbe.这是我看到关于求next数组,解释最好的一篇文章!!!!!!! KMP的next数组求法是很不容易搞清楚的一部 ...