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' ...
随机推荐
- 搭建开发环境3) Debian8 安装Netbeans
1.下载最新版的Netbeans.sh(netbeans-8.2-linux.sh) 2.进入管理员账户,移动到下载的目录 ./netbeans-8.2-linux.sh //执行安装脚本 出现界面安 ...
- 使用JBolt新建Maven版工程步骤
一.打开新建对话框 在左侧右键new中可以找到JFinal创建工程的菜单 JBoltHome页面也有快捷按钮用来弹出创建工程对话框. 二.填写Maven和其他信息配置 填写工程name 主包名 下面有 ...
- APIPA
自动专用IP地址(Automatic Private IP Address,APIPA)是当客户端无法从DHCP服务器中获得IP地址时自动配置的地址.IPv4地址前缀169.254/16已经被IANA ...
- 链路聚合trunk实现
用户需求 1,在原有网络基础上实现用户接入Internet 2,监控摄像头不改变原有功能 配置思路 1,首先确定接入交换机是否为管理型交换机 2, 确认接入交换机管理IP 3,划分Vlan 创建tr ...
- JS 实现Table相同行的单元格自动合并示例代码
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD ...
- js正則匹配经纬度(经纬度逗号隔开)
谷歌坐標:31.2807691689,112.5382624525 高德坐標:31.2807691689,112.5382624525 regexp: {//正则验证 regexp: /^([0-9] ...
- spring事务详解(二)简单样例
系列目录 spring事务详解(一)初探事务 spring事务详解(二)简单样例 spring事务详解(三)源码详解 spring事务详解(四)测试验证 spring事务详解(五)总结提高 一.引子 ...
- bsdiff差分算法
bsdiff的基本原理 bsdiff是由Conlin Percival开源的一个优秀的差分算法,而且是跨平台的.在Android系统中所使用的imgdiff本质上就是bsdiff. bsdiff的依据 ...
- MySQL报错ERROR 1558 (HY000): Column count of mysql.user is wrong.
MySQL报错ERROR 1558 (HY000): Column count of mysql.user is wrong. 1.今天在使用MySQL创建数据库时出现如下报错: mysql> ...
- centos 7下rabbitmq安装(转)
安装erlang环境 添加rabbitmq依赖的erlang yum命令repos # In /etc/yum.repos.d/rabbitmq-erlang.repo [rabbitmq-erlan ...