xml和configparser模块
一、xml模块
xml是实现不同语言或程序之间进行数据交换的协议,跟json差不多,但json使用起来更简单,
但至今很多传统公司如金融行业的很多系统的接口还主要是xml。
xml的格式如下,就是通过<>节点来区别数据结构的:
<?xml version="1.0"?>
<data>
<country name="Liechtenstein">
<rank updated="yes">2</rank>
<year>2008</year>
<gdppc>141100</gdppc>
<neighbor name="Austria" direction="E"/>
<neighbor name="Switzerland" direction="W"/>
</country>
<country name="Singapore">
<rank updated="yes">5</rank>
<year>2011</year>
<gdppc>59900</gdppc>
<neighbor name="Malaysia" direction="N"/>
</country>
<country name="Panama">
<rank updated="yes">69</rank>
<year>2011</year>
<gdppc>13600</gdppc>
<neighbor name="Costa Rica" direction="W"/>
<neighbor name="Colombia" direction="E"/>
</country>
</data>
xml协议在各个语言里的都 是支持的,在python中可以用以下模块操作xml
# _*_coding:utf-8_*_ import xml.etree.ElementTree as ET
# xml 通过<> 节点区别数据结构 tree = ET.parse("xml test") # open 文件
root = tree.getroot() #f.seek(0)
#print(dir(root))
print(root.tag) # 打印标签
#
#遍历xml文档
for child in root:
print('----------',child.tag, child.attrib)
for i in child:
print(i.tag,i.text) #只遍历year 节点
for node in root.iter('year'):
print(node.tag,node.text)
修改和删除xml文档内容
# _*_coding:utf-8_*_
import xml.etree.ElementTree as ET
tree = ET.parse("xml test")
root = tree.getroot() #f.seek(0)
#修改
for node in root.iter('year'):
new_year = int(node.text) - 1
node.text = str(new_year) # 必须是字符串
node.set("revise_test","yes")
#删除node
for country in root.findall('country'):
rank = int(country.find('rank').text)
if rank > 50:
root.remove(country)
tree.write('output.xml')
自己创建xml文档
import xml.etree.ElementTree as ET
new_xml = ET.Element("namelist")
name = ET.SubElement(new_xml,"name",attrib={"enrolled":"yes"})
age = ET.SubElement(name,"age",attrib={"checked":"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) #打印生成的格式
二、configparser模块
configparser模块用于生成和修改常见配置文档。
常见配置文件格式如下:
[DEFAULT]
ServerAliveInterval = 45
Compression = yes
CompressionLevel = 9
ForwardX11 = yes [bitbucket.org]
User = hg [topsecret.server.com]
Port = 50022
ForwardX11 = no
解析配置文件
#!/usr/bin/env python3
#-*- coding:utf-8 -*- import configparser conf = configparser.ConfigParser()
#print(conf.sections())
conf.read('conf.ini')
print(conf.sections())
print(conf.default_section) # DEFAULT
print(conf['bitbucket.org']['user']) # hg print(list(conf['bitbucket.org'].keys())) #
# ['user', 'maxusers', 'compression', 'compressionlevel', 'serveraliveinterval', 'forwardx11'] for k,v in conf['bitbucket.org'].items():
print(k,v)
'''
user hg
maxusers 100
compression yes
compressionlevel 9
serveraliveinterval 45
forwardx11 yes
'''
# default 中的参数是默认每个里面都有的 if 'user' in conf['bitbucket.org']:
print('it is right')
增删改查语法
#!/usr/bin/env python3
#-*- coding:utf-8 -*- import configparser
conf = configparser.ConfigParser() conf.read('conf_test.ini')
print(dir(conf)) # 查
print(conf.options('group1')) # ['k1', 'k2']
print(conf['group1']['k1']) # v1
print(conf.get('group1','k1')) # v1 # 增加
conf.add_section('group3') # 添加节点
conf['group3']['name'] = 'cc'
conf['group3']['age'] = '21 ' # 必须为字符串
conf.write(open('conf_test_new.ini','w')) # 改
conf.set('group2','k1','')
conf.write(open('conf_test_new.ini','w')) # 删
conf.remove_option('group1','k2')
conf.remove_section('group2')
conf.write(open('conf_test_new.ini','w'))
xml和configparser模块的更多相关文章
- 第二十一天,pickle json xml shelve configparser模块
今日内容 1.pcikle 专用于python语言的序列化 2.json 是一种跨平台的数据格式 也属于序列化的一种方式 3.xml 可拓展标记语言 一种编写文档的语法 也支持跨平台 比较json而言 ...
- python之shelve、xml、configparser模块
一.shelve模块 shelve模块比pickle模块简单,只有一个open函数,返回类似字典的对象,可读可写;key必须为字符串,而值可以是python所支持的数据类型 import shelve ...
- python模块基础之json,requeste,xml,configparser,logging,subprocess,shutil。
1.json模块 json 用于[字符串]和 [python基本数据类型] 间进行转换(可用于不同语言之前转换),json.loads,将字符串转成python的基本数据类型,json.dum ...
- Python之xml文档及配置文件处理(ElementTree模块、ConfigParser模块)
本节内容 前言 XML处理模块 ConfigParser/configparser模块 总结 一.前言 我们在<中我们描述了Python数据持久化的大体概念和基本处理方式,通过这些知识点我们已经 ...
- Learning-Python【20】:Python常用模块(3)—— shelve、pickle、json、xml、configparser
什么是序列化/反序列化? 序列化就是将内存中的数据结构转换成一种中间格式存储到硬盘或者基于网络传输,反序列化就是硬盘中或者网络中传来的一种数据格式转换成内存中数据结构 为什么要有序列化/反序列化? 1 ...
- 【转】Python之xml文档及配置文件处理(ElementTree模块、ConfigParser模块)
[转]Python之xml文档及配置文件处理(ElementTree模块.ConfigParser模块) 本节内容 前言 XML处理模块 ConfigParser/configparser模块 总结 ...
- 常用模块之 shutil,json,pickle,shelve,xml,configparser
shutil 高级的文件.文件夹.压缩包 处理模块 shutil.copyfileobj(fsrc, fdst[, length]) 将文件内容拷贝到另一个文件中 import shutil shut ...
- Python hash、xml、configparser、sheve、shutil模块讲解 以及 面向对象初识
今日内容: 1.hash模块2.xml模块3.configparser模块4.sheve 模块5.shutil模块 知识点一:hash什么是hash: hash是一种算法,该算法接受传入的的内容,经过 ...
- python常用模块:pickle、shelve、json、xml、configparser
今日内容主要有: 一.pickle模块二.shelve模块三.json模块四.json练习五.xml模块 六.xml练习七.configparser模块 一.pickle模块 #pickle是一个用来 ...
随机推荐
- .net 学习路线感想(转)
从上到大学到现在工作,已经有六年多了,发现学习编程到以开发为工作也是一个挺长的过程的. 大学中,从c语言到java.C#到其他各种语言的学习,还有其他知识的学习如:数据库(oracle.sql Ser ...
- Linux系统下的shutdown命令用于安全的关闭/重启计算机
Linux系统下的shutdown命令用于安全的关闭/重启计算机,它不仅可以方便的实现定时关机,还可以由用户决定关机时的相关参数.在执行shutdown命令时,系统会给每个终端(用户)发送一条屏显,提 ...
- php之接口内curl请求其他接口
今天遇到一个需要写curl的需求,情况是这样的: 同一应用的A系统(购物系统),B系统(答题系统)相互独立,用户数据全部存在于A系统的数据库中, 现在处于B系统的某项操作中,需要在B系统中验证当前请求 ...
- c++ 字符串查找函数
头文件:#include <string.h> 定义函数:int strcasecmp (const char *s1, const char *s2); 函数说明:strcasecmp( ...
- HihoCoder1622 : 有趣的子区间(预处理+组合数)
有趣的子区间 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 如果一个区间[a, b]内恰好包含偶数个回文整数,我们就称[a, b]是有趣的区间. 例如[9, 12]包含 ...
- Sublimetext3插件与使用技巧
1. package control 的安装与注意事项 2. 常用插件的安装与注意事项 3. 主题风格设置 4. 常用快捷键 https://packagecontrol.io ...
- Ctrl+H 浪潮Raid配置文档
说明 本手册适用于LSI芯片Raid卡 包括但不限于Inspur 2008/2108 Raid卡.LSI 9240/9260/9261/9271 等Raid卡. 不同型号的Raid卡在某些功能上的支持 ...
- bzoj 3779 重组病毒 —— LCT+树状数组(区间修改+区间查询)
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3779 RELEASE操作可以对应LCT的 access,RECENTER则是 makeroo ...
- BMP格式介绍(一)
原理篇: 一.编码的意义. 让我们从一个简单的问题开始,-2&-255(中间的操作符表示and的意思)的结果是多少,这个很简单的问题,但是能够写出解答过程的人并不 多.这个看起来和图片格式没有 ...
- DSP SYS/BIOS开发
https://blog.csdn.net/lg1259156776/article/details/80695318