首页 技术 正文
技术 2022年11月14日
0 收藏 390 点赞 2,841 浏览 2797 个字

操作键值对文件

 #文件db格式为
[section]
a = 1
b = 2 [section1]
d = 3
c = 4 import configparser #获取所有节点
config = configparser.ConfigParser()
config.read('db')
ret = config.sections()
print(ret) >>>['section', 'section1'] #获取指定节点下所有的键值对
ret = config.items('section1')
print('获取键值对:',ret)
>>>获取键值对: [('d', ''), ('c', '')] #获取所有的键
ret = config.options('section')
print('获取节点下键名称:',ret)
>>>获取节点下键名称: ['a', 'b'] #获取节点下的指定key的值
ret =config.get('section1','c')
print(ret)
>>>4 #检查节点是否存在
has_sec = config.has_section('section1')
print(has_sec)
>>>True #添加节点section2
config.add_section('section2')
config.write(open('db','w')) #db文件当前为
[section]
a = 1
b = 2 [section1]
d = 3
c = 4 [section2] #删除节点section2
config.remove_section('section2')
config.write(open('db','w')) #当前db文件内容为
[section]
a = 1
b = 2 [section1]
d = 3
c = 4 #检查删除设置指定组内的键值对是否存在
has_opt = config.has_option('section1','c')
print(has_opt)
>>>True #删除
config.remove_option('section1','c')
config.write(open('db','w'))
#当前db文件内容为
[section]
a = 1
b = 2 [section1]
d = 3 #设置更改指定值
config.set('section1','d','')
config.write(open('db','w'))
#db内容
[section]
a = 1
b = 2 [section1]
d = 123
c = 4
 #oo.xml文件内容
<data>
<country name="Liechtenstein">
<rank updated="yes">2</rank>
<year>2023</year>
<year>2027</year>
<year>2027</year>
<gdppc>141100</gdppc>
<neighbor direction="E" name="Austria" />
<neighbor direction="W" name="Switzerland" />
</country>
</data> from xml.etree import ElementTree as ET #打开xml文件加载到内存
tree = ET.parse('oo.xml')
#获取根节点
root = tree.getroot() for child in root:
print(child.tag,child.attrib)#child.tag,子节点标签名,子节点属性
for gradechild in child:
print(gradechild.tag,gradechild.text,gradechild.attrib) >>>country {'name': 'Liechtenstein'}
>>>rank 2 {'updated': 'yes'}
>>>year 2023 {}
>>>gdppc 141100 {}
>>>neighbor None {'direction': 'E', 'name': 'Austria'}
>>>neighbor None {'direction': 'W', 'name': 'Switzerland'} #两种方法读取xml文件直接解析,tree对象是ElementTree对象
tree = ET.parse('oo.xml')
root = tree.getroot() #将xml已字符串格式获得,将字符串转化为element对象,无法进行写操作
str_xml = open('oo.xml','r').read()
root = ET.XML(str_xml) #循环所有的year节点
for node in root.iter('year'):
#将year节点中的内容自增一
new_year = int (node.text) +1
node.text = str(new_year)
print(node.text)
>>>2024
>>>2027
>>>2027
#设置属性
node.set('name','alex')
node.set('age','')
print(node.attrib)
>>>{'age': '', 'name': 'alex'}
>>>{'age': '', 'name': 'alex'}
>>>{'age': '', 'name': 'alex'}
#删除属性
del node.attrib['name']
print(node.attrib)
>>>{'age': ''}
>>>{'age': ''}
>>>{'age': ''}
#保存文件
tree = ET.ElementTree(root)
tree.write("newnew.xml", encoding='utf-8')
 #创建根节点
root = ET.Element('family') #创建大儿子
son1 = ET.Element('son',{'name':'儿子1'})
#创建二儿子
son2 = ET.Element('son',{'name':'儿子2'}) #大儿子中创建两个孙子
grandson1 = ET.Element('grandson',{'name':'孙子1'})
grandson2 = ET.Element('grandson',{'name':'孙子2'})
son1.append(grandson1)
son2.append(grandson2) root.append(son1)
root.append(son2) tree = ET.ElementTree(root)
tree.write('ooo.xml',encoding = 'utf-8',short_empty_elements = False)
#ooo.xml文件为
<family><son name="儿子1"><grandson name="孙子1"></grandson></son><son name="儿子2"><grandson name="孙子2"></grandson></son></family>
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,083
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,558
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,407
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,180
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,817
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,900