操作键值对文件
1 #文件db格式为
2 [section]
3 a = 1
4 b = 2
5
6 [section1]
7 d = 3
8 c = 4
9
10 import configparser
11
12 #获取所有节点
13 config =
configparser.ConfigParser()
14 config.read(
'db')
15 ret =
config.sections()
16 print(ret)
17
18 >>>[
'section',
'section1']
19
20 #获取指定节点下所有的键值对
21 ret = config.items(
'section1')
22 print(
'获取键值对:',ret)
23 >>>获取键值对: [(
'd',
'3'), (
'c',
'4')]
24
25 #获取所有的键
26 ret = config.options(
'section')
27 print(
'获取节点下键名称:',ret)
28 >>>获取节点下键名称: [
'a',
'b']
29
30 #获取节点下的指定key的值
31 ret =config.get(
'section1',
'c')
32 print(ret)
33 >>>4
34
35 #检查节点是否存在
36 has_sec = config.has_section(
'section1')
37 print(has_sec)
38 >>>
True
39
40 #添加节点section2
41 config.add_section(
'section2')
42 config.write(open(
'db',
'w'))
43
44 #db文件当前为
45 [section]
46 a = 1
47 b = 2
48
49 [section1]
50 d = 3
51 c = 4
52
53 [section2]
54
55 #删除节点section2
56 config.remove_section(
'section2')
57 config.write(open(
'db',
'w'))
58
59 #当前db文件内容为
60 [section]
61 a = 1
62 b = 2
63
64 [section1]
65 d = 3
66 c = 4
67
68 #检查删除设置指定组内的键值对是否存在
69 has_opt = config.has_option(
'section1',
'c')
70 print(has_opt)
71 >>>
True
72
73 #删除
74 config.remove_option(
'section1',
'c')
75 config.write(open(
'db',
'w'))
76 #当前db文件内容为
77 [section]
78 a = 1
79 b = 2
80
81 [section1]
82 d = 3
83
84 #设置更改指定值
85 config.set(
'section1',
'd',
'123')
86 config.write(open(
'db',
'w'))
87 #db内容
88 [section]
89 a = 1
90 b = 2
91
92 [section1]
93 d = 123
94 c = 4
1 #oo.xml文件内容
2 <data>
3 <country name=
"Liechtenstein">
4 <rank updated=
"yes">2</rank>
5 <year>2023</year>
6 <year>2027</year>
7 <year>2027</year>
8 <gdppc>141100</gdppc>
9 <neighbor direction=
"E" name=
"Austria" />
10 <neighbor direction=
"W" name=
"Switzerland" />
11 </country>
12 </data>
13
14 from xml.etree
import ElementTree as ET
15
16 #打开xml文件加载到内存
17 tree = ET.parse(
'oo.xml')
18 #获取根节点
19 root =
tree.getroot()
20
21 for child
in root:
22 print(child.tag,child.attrib)
#child.tag,子节点标签名,子节点属性
23 for gradechild
in child:
24 print(gradechild.tag,gradechild.text,gradechild.attrib)
25
26 >>>country {
'name':
'Liechtenstein'}
27 >>>rank 2 {
'updated':
'yes'}
28 >>>year 2023
{}
29 >>>gdppc 141100
{}
30 >>>neighbor None {
'direction':
'E',
'name':
'Austria'}
31 >>>neighbor None {
'direction':
'W',
'name':
'Switzerland'}
32
33 #两种方法读取xml文件直接解析,tree对象是ElementTree对象
34 tree = ET.parse(
'oo.xml')
35 root =
tree.getroot()
36
37 #将xml已字符串格式获得,将字符串转化为element对象,无法进行写操作
38 str_xml = open(
'oo.xml',
'r').read()
39 root =
ET.XML(str_xml)
40
41 #循环所有的year节点
42 for node
in root.iter(
'year'):
43 #将year节点中的内容自增一
44 new_year = int (node.text) +1
45 node.text =
str(new_year)
46 print(node.text)
47 >>>2024
48 >>>2027
49 >>>2027
50 #设置属性
51 node.set(
'name',
'alex')
52 node.set(
'age',
'18')
53 print(node.attrib)
54 >>>{
'age':
'18',
'name':
'alex'}
55 >>>{
'age':
'18',
'name':
'alex'}
56 >>>{
'age':
'18',
'name':
'alex'}
57 #删除属性
58 del node.attrib[
'name']
59 print(node.attrib)
60 >>>{
'age':
'18'}
61 >>>{
'age':
'18'}
62 >>>{
'age':
'18'}
63 #保存文件
64 tree =
ET.ElementTree(root)
65 tree.write(
"newnew.xml", encoding=
'utf-8')
1 #创建根节点
2 root = ET.Element(
'family')
3
4 #创建大儿子
5 son1 = ET.Element(
'son',{
'name':
'儿子1'})
6 #创建二儿子
7 son2 = ET.Element(
'son',{
'name':
'儿子2'})
8
9 #大儿子中创建两个孙子
10 grandson1 = ET.Element(
'grandson',{
'name':
'孙子1'})
11 grandson2 = ET.Element(
'grandson',{
'name':
'孙子2'})
12 son1.append(grandson1)
13 son2.append(grandson2)
14
15 root.append(son1)
16 root.append(son2)
17
18 tree =
ET.ElementTree(root)
19 tree.write(
'ooo.xml',encoding =
'utf-8',short_empty_elements =
False)
20 #ooo.xml文件为
21 <family><son name=
"儿子1"><grandson name=
"孙子1"></grandson></son><son name=
"儿子2"><grandson name=
"孙子2"></grandson></son></family>
转载于:https://www.cnblogs.com/liguangxu/p/5605157.html