python configparse模块&xml模块
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 = ""
name2 = ET.SubElement(new_xml,"name",attrib={"enrolled": "no"})
age = ET.SubElement(name2,"age")
age.text = ""
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模块的更多相关文章
- 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标准库]XML模块
1.什么是XML XML是可扩展标记语言(Extensible Markup Language)的缩写,其中的 标记(markup)是关键部分.您可以创建内容,然后使用限定标记标记它,从而使每个单词. ...
- 人生苦短之我用Python篇(XML模块)
XML模块 http://baike.baidu.com/link?url=-mBgvMdEDU7F05Pw7h_hBt7A0ctYiPm5a_WvKVLknydnRXKRIyydcVZWRjd_5H ...
- python笔记7 logging模块 hashlib模块 异常处理 datetime模块 shutil模块 xml模块(了解)
logging模块 日志就是记录一些信息,方便查询或者辅助开发 记录文件,显示屏幕 低配日志, 只能写入文件或者屏幕输出 屏幕输出 import logging logging.debug('调试模式 ...
- python学习-52 XML模块
XML模块 xml是实现不同语言或程序之间进行数据交换的协议,跟json差不多,但是json使用起来更简单. 例如:创建一个xml文件 <data> <country name=&q ...
- s14 第5天 时间模块 随机模块 String模块 shutil模块(文件操作) 文件压缩(zipfile和tarfile)shelve模块 XML模块 ConfigParser配置文件操作模块 hashlib散列模块 Subprocess模块(调用shell) logging模块 正则表达式模块 r字符串和转译
时间模块 time datatime time.clock(2.7) time.process_time(3.3) 测量处理器运算时间,不包括sleep时间 time.altzone 返回与UTC时间 ...
- python的内置模块xml模块方法 xml解析 详解以及使用
一.XML介绍 xml是实现不同语言或程序直接进行数据交换的协议,跟json差不多,单json使用起来更简单,不过现在还有很多传统公司的接口主要还是xml xml跟html都属于是标签语言 我们主要学 ...
- python之路xml模块补充
创建一个子节点一共有三个方式 创建一个子节点2.3
- shelve模块 xml模块
# import shelve# f=shelve.open('db.shl')# # f['stu1']={'name':'alex1','age':28}# # f['stu2']={'name' ...
随机推荐
- .net core+Spring Cloud学习之路 二
前言: 原本计划这次写一下搭建eureka群集.但是发现上次写的只是服务的注册,忘了写服务的发现,所以这次先把服务发现补上去. 我们基于上篇文章,再新建两个.net core web api项目,分别 ...
- GitHub Desktop下载及使用
GitHub Desktop下载及使用 用了几次 GitHub Desktop 之后,发现不好用,其图形化界面功能有限.推荐使用Git for Windows,官方网站 https://git-f ...
- C# Cookie方法
//写入 protected void Button1_Click(object sender, EventArgs e) { HttpCookie cookie=new HttpCookie(&qu ...
- Web前端学习第一天
1.SQL注入攻击的发生 select username, email ,descl from users where id=1; 可能会被伪造成 1 union select password,1, ...
- Exception和解决方案
org.hibernate.HibernateException: No Session found for current thread sessionFactory org.springframe ...
- 芯灵思Sinlinx A64 Linux&qt编译安装
开发平台 芯灵思Sinlinx A64 内存: 1GB 存储: 4GB 详细参数 https://m.tb.cn/h.3wMaSKm 开发板交流群 641395230 前提条件搭建好CentOS环境 ...
- dubbo 中文官网
根大家分享一下:dubbo的中文官网迁移到了githup上地址:https://dubbo.gitbooks.io/dubbo-user-book/content/preface/background ...
- Jmeter 传 PUT 请求方式
最近用 Jmeter 发送 PUT 请求,踩了个坑,现记录如下: 难点在在于 body 内有一大串 json 形式的内容 1.PUT 请求的 body 内,直接将 json串传 form-data 形 ...
- python2和3的区别
一.默认编码 2:ascii 3:utf-8 二.数字 python3无long
- Centos 7 修改日期和时间的命令
timedatectl set-ntp no //关闭时间动态更新timedatectl set-time "YYYY-MM-DD HH:MM:SS" //设置时间和日期timed ...