1. 程式人生 > >python configparse模塊&xml模塊

python configparse模塊&xml模塊

count 讀取配置文件 move 支持 調用 金融 bucket for ali

configparse模塊

用於生成和修改常見配置文檔,當前模塊的名稱在 python 3.x 版本中變更為 configparser。

[DEFAULT]
serveraliveinterval = 45
compression = yes
compressionlevel = 9
forwardx11 = yes

[bitbucket.org]
user = hg

[topsecret.server.com]
port = 50022
forwardx11 = no

[group]

import configparser
# ------------------------------------讀----------------------------------
# 讀取文件內容 config = configparser.ConfigParser() # 實例化(生成對象) config.read("config_file.cnf", encoding="utf-8") # 讀取配置文件 ret = config.sections() # 調用sections方法(默認不會讀取default) print(ret) print("bitbucket.org" in config) # 判斷元素是否在sections列表內
print(config["bitbucket.org"]["user"]) # 通過字典的形式取值 for i in config["topsecret.server.com"]: # for 循環"topsecret.server.com"字典中key print(i) for i,v in config["topsecret.server.com"].items(): # 使用items函數循環"topsecret.server.com"字典的鍵和值 print(i,v) # 還有另一種讀法 options = config.options("
bitbucket.org") # 獲取指定section的keys,(default 下的key默認讀取) print(options) item_li = config.items("bitbucket.org") # 使用items獲取指定section的鍵和值,元祖的形式,(default 下的key默認讀取) print(item_li) val = config.get("bitbucket.org", "user") # 使用get獲取指定鍵的值 print(val) # -------------------------改---------------------------- # config.add_section("group") # 新增一個sections,指定的section已存在時,報錯 #has = config.has_section("group",name) # 檢查 # print(has) # config.set("group", "name", "sb") # 修改指定的section下的指定的鍵的值,若鍵不存在,自動添加 # config.add_section("group1") config.remove_section("group1") # 刪除指定的sections ,sections不存在不會報錯 config.remove_option("group","name") # 刪除指定的sections下指定的鍵值對 config.write(open("config_file.cnf", "w")) # 修改後寫入文件生效

xml模塊

xml是實現不同語言或程序之間進行數據交換的協議,跟json差不多,但json使用起來更簡單,不過,古時候,在json還沒誕生的黑暗年代,大家只能選擇用xml呀,至今很多傳統公司如金融行業的很多系統的接口還主要是xml。

<data>
    <country name="Liechtenstein">
        <rank updated="yes">2</rank>
        <year update="yes">2016</year>
        <gdppc>141100</gdppc>
        <neighbor direction="E" name="Austria" />
        <neighbor direction="W" name="Switzerland" />
    </country>
    <country name="Singapore">
        <rank updated="yes">5</rank>
        <year update="yes">2019</year>
        <gdppc>59900</gdppc>
        <neighbor direction="N" name="Malaysia" />
    </country>
    <country name="Panama">
        <rank updated="yes">69</rank>
        <year update="yes">2019</year>
        <gdppc>13600</gdppc>
        <neighbor direction="W" name="Costa Rica" />
        <neighbor direction="E" name="Colombia" />
    </country>
</data>

# xml協議在各個語言裏的都 是支持的,在python中可以用以下模塊操作xml
import xml.etree.ElementTree as ET
tree = ET.parse("xml_file.xml")
root = tree.getroot()
print(root.tag)
# 遍歷xml文檔
for xx in root:
    print(xx.tag, xx.attrib)
    for i in xx:
        print(i.tag,i.text)
        
# 只遍歷年
for i in root.iter("year"):
    print(i.tag,i.text)
# 修改
for i in root.iter("year"):
    new_year = int(i.text) + 1
    i.text = str(new_year)
    i.set("update","yes")
tree.write("xml_file.xml")
# 刪除
for country in root.findall("country"):
    rank = int(country.find("rank").text)
    if rank >= 50:
        root.remove(country)
tree.write("out_file.xml")

創建一個xml文件

# 創建一個xml
new_xml = ET.Element("name_list")
name = ET.SubElement(new_xml, "name",attrib={"enrolled":"yes"})
age = ET.SubElement(name,"age",attrib={"cheked": "no"})
sex = ET.SubElement(name,"sex")
sex.text = "33"
name2 = ET.SubElement(new_xml,"name",attrib={"enrolled": "no"})
age = ET.SubElement(name2,"age")
age.text = "19"
et = ET.ElementTree(new_xml)
et.write("test.xml",encoding="utf-8",xml_declaration=True)
ET.dump(new_xml)
<?xml version=1.0 encoding=utf-8?>
<name_list><name enrolled="yes"><age cheked="no" /><sex>33</sex></name><name enrolled="no"><age>19</age></name></name_list>

python configparse模塊&xml模塊