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是一个用来 ...
随机推荐
- pg确定一张表最后被使用的时间
create or replace function table_file_access_info( IN schemaname text,IN tablename text,OUT last_acc ...
- XE7/10诡异报错brcc32错误
重新编译工程时,报错: 之前没遇到过,解决方法: 重新设置下Application Icon,再build,问题解决.
- .NET面试题总结
1.c#垃圾回收机制 从以下方面入手展开: 1.压缩合并算法 2.代的机制 3.GC调用终结器 2.委托和事件 先说它的定义:委托的本质是类,类型安全的指针,然后从用途上考虑,事件是包装的委托 ...
- 关于modelsim添加库的说明
声明:以下纯属个人习惯. 1.工程建好后添加一个编译好的库的方法是:file->new->library选择a map to an existing library.然后将这个库在你这工程 ...
- 笔记:LIR2032 电池充电记录
笔记:LIR2032 电池充电记录 LIR2032 电池是锂电池,形状和 CR2032 一样,只不过可以充电,材料是锂离子. 一个单颗的 LIR2032 电池容量只有 40mAH,容量很小. 那么就需 ...
- webstorm设置修改文件后自动编译并刷新浏览器页面
转载:http://www.cnblogs.com/ssrsblogs/p/6155747.html 重装了 webstorm ,从10升级到了2016 一升不要紧,打开老项目,开启webpakc-d ...
- rbenv配置
git clone https://github.com/rbenv/rbenv.git ~/.rbenv # 用来编译安装 ruby git clone git://github.com/sstep ...
- java中求输入一个数,并计算其平方根~~~
总结:函数 Math.pow(x,0.5); package com.badu; import java.util.Scanner; // 输入一个数,并计算出平方根 public class AA ...
- 1112 Stucked Keyboard
题意:坏掉的键若被按下,总是重复打出k次.比如,k为3,打出的序列如下—— thiiis iiisss a teeeeeest 坏掉的键是i和e,虽然iiisss中s也出现了3次,但它不是坏掉的键,因 ...
- python学习(十五) 屏幕抓取
15.1 屏幕抓取 15.1.1 Tidy和XHTML解析 Tidy:用来修复不规范且随意的HTML文档的工具. 为什么用XHTML: 和旧版本的HTML之间最主要的区别:HTML可能只用一个开始标签 ...