python不同模块间传递数据_Python模块-数据传送模块
json
數(shù)據(jù)交換,可以跨語(yǔ)言交換,比如php、java等,默認(rèn)使用規(guī)范為雙引號(hào)。
json類(lèi)型
Python類(lèi)型
{}
dict
[]
list
"str"
str
123.45
int或float
true/flase
True/Flase
null
None
json.dumps()
序列號(hào)操作,轉(zhuǎn)換為json格式字符串
普通字典轉(zhuǎn)換為字符串做法
# -*- coding: -*-
dic = {'a':"1"}
a = str(dic) #數(shù)據(jù)轉(zhuǎn)換
print("原來(lái)數(shù)據(jù)類(lèi)型:",type(dic))
print("轉(zhuǎn)換后數(shù)據(jù)類(lèi)型:",type(a))
原來(lái)數(shù)據(jù)類(lèi)型:
轉(zhuǎn)換后數(shù)據(jù)類(lèi)型:
普通字典樣式的字符串轉(zhuǎn)字典示例做法
# -*- coding: -*-
A = "{'a':1}"
B = eval(A) #數(shù)據(jù)轉(zhuǎn)換
print("原來(lái)數(shù)據(jù)類(lèi)型:",type(A))
print("轉(zhuǎn)換后數(shù)據(jù)類(lèi)型:",type(B))
json 字典轉(zhuǎn)換字符串
字符串單引號(hào)變雙引號(hào),雙引號(hào)字符串保持不變
import json
dic = {'name':1}
print("轉(zhuǎn)換結(jié)果:",json.dumps(dic))
print("轉(zhuǎn)換結(jié)果類(lèi)型:",type(json.dumps(dic)))
轉(zhuǎn)換結(jié)果: {"name": 1}
轉(zhuǎn)換結(jié)果類(lèi)型:
json.loads()
反序列化操作,將json格式字符串轉(zhuǎn)換為原來(lái)的類(lèi)型
import json
dic = {'name':1}
a = json.dumps(dic)
#以上為字典轉(zhuǎn)換為json格式的字符串
print("轉(zhuǎn)換結(jié)果:",json.loads(a))
print("轉(zhuǎn)換結(jié)果類(lèi)型:",type(json.loads(a)))
轉(zhuǎn)換結(jié)果: {'name': 1}
轉(zhuǎn)換結(jié)果類(lèi)型:
Pickle模塊
json對(duì)比區(qū)別
pickle寫(xiě)入內(nèi)容不可讀,pickle支持的數(shù)據(jù)類(lèi)型更多
引用
import pickle
方法
轉(zhuǎn)換為字符串類(lèi)型
pickle.dump
pickle.dumps
轉(zhuǎn)換為原來(lái)類(lèi)型
pickle.load
pickle.loads
shelve模塊
shelves寫(xiě)入內(nèi)容不可讀
序列化操作存儲(chǔ)
import shelve
a = shelve.open("test")
a["name"] = {"xiaowang"}
自動(dòng)生成的三個(gè)文件
test.bak
test.dat
test.dir
反序列化操作讀取
import shelve
a = shelve.open("test")
a["name"] = {"xiaowang"}
print(a.get("name"))
{'xiaowang'}
xml模塊,ElementTree解析
測(cè)試文件
1
2
3
4
5
標(biāo)簽名稱(chēng)
文檔樹(shù)的根節(jié)點(diǎn)標(biāo)簽名稱(chēng)
import xml.etree.ElementTree as ET
tree = ET.parse("XMLConfig.xml") #文檔樹(shù)內(nèi)存地址
tree_root = tree.getroot() #文檔樹(shù)根的內(nèi)存地址
print(tree_root.tag) #文檔樹(shù)根的標(biāo)簽
root
標(biāo)簽屬性
文檔樹(shù)中各個(gè)屬性以字典鍵值對(duì)形式輸出
# -*- coding: utf8 -*-
import xml.etree.ElementTree as ET
tree = ET.parse("XMLConfig.xml") #文檔樹(shù)內(nèi)存地址
tree_root = tree.getroot() #文檔樹(shù)根的內(nèi)存地址
for i in tree_root:
print(i.attrib) #文檔樹(shù)中的分類(lèi)列表
{'dec': 'list_1'}
{'dec': 'list_2'}
{'dec': 'list_3'}
{'dec': 'list_4'}
{'dec': 'list_5'}
標(biāo)簽內(nèi)容
# -*- coding: utf8 -*-
import xml.etree.ElementTree as ET
tree = ET.parse("XMLConfig.xml") #文檔樹(shù)內(nèi)存地址
tree_root = tree.getroot() #文檔樹(shù)根的內(nèi)存地址
for i in tree_root: #遍歷文檔樹(shù)
for j in i: #遍歷文檔樹(shù)列表
print(j.text) #文檔樹(shù)遍歷文檔列表中標(biāo)簽內(nèi)容
1
None
None
2
None
None
3
4
None
5
None
None
修改內(nèi)容
# -*- coding: utf8 -*-
import xml.etree.ElementTree as ET
tree = ET.parse("XMLConfig.xml") #文檔樹(shù)內(nèi)存地址
tree_root = tree.getroot() #文檔樹(shù)根的內(nèi)存地址
for i in tree_root.iter("test"): #遍歷文檔樹(shù)
i.set("test", "new_txt") #文檔樹(shù)遍歷文檔列表中標(biāo)簽內(nèi)容 修改
tree.write("XMLConfig2.xml") #保存修改后的文件
修改后
1
2
3
4
5
刪除內(nèi)容
import xml.etree.ElementTree as ET
tree = ET.parse("XMLConfig.xml") #文檔樹(shù)內(nèi)存地址
tree_root = tree.getroot() #文檔樹(shù)根的內(nèi)存地址
for i in tree_root.findall("GV"): #遍歷文檔樹(shù)第一層
rank = int(i.find("test").text) #第二層查找標(biāo)簽并賦值
if rank > 4: #第二層條件判斷
tree_root.remove(i) #不符合條件刪除操作
tree.write("XMLConfig2.xml") #保存修改后的文件
刪除后
1
2
3
4
生成xml文件
# -*- coding: utf8 -*-
import xml.etree.ElementTree as ET
new_xml = ET.Element("xml_list") #創(chuàng)建文檔樹(shù)根節(jié)點(diǎn)
name = ET.SubElement(new_xml,"name",attrib={"test1":"1"}) #在根節(jié)點(diǎn)中插入子元素
name = ET.SubElement(new_xml,"name",attrib={"test2":"2"}) #在根節(jié)點(diǎn)中插入子元素
et = ET.ElementTree(new_xml) #生成文檔對(duì)象
ET.dump(new_xml) #打印生成的格式
et.write("test.xml",encoding="utf8",xml_declaration=True) #生成文件
打印出的內(nèi)容
生成的文件內(nèi)容
總結(jié)
以上是生活随笔為你收集整理的python不同模块间传递数据_Python模块-数据传送模块的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: iconnect是什么软件
- 下一篇: 认识python编程环境_认识Pytho