1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
/rank
>
/year
>
/gdppc
>
<
/country
>
/rank
>
/year
>
/gdppc
>
<
/country
>
/rank
>
/year
>
/gdppc
>
<
/country
>
<
/data
>
|
1
2
3
|
from
xml.etree.ElementTree
import
parse
tree
=
parse(
'demo.xml'
)
/
/
获取ElementTree
root
=
tree.getroot()
/
/
获取根元素
|
1
2
3
4
5
6
7
8
|
In [
6
]: root.tag
Out[
6
]:
'data'
In [
7
]: root.attrib
Out[
7
]: {}
In [
25
]: root.text
Out[
25
]:
'\n '
|
1
2
3
4
5
6
|
In [
8
]:
for
child
in
root:
...:
print
(child.tag, child.attrib)
...:
country {
'name'
:
'Liechtenstein'
}
country {
'name'
:
'Singapore'
}
country {
'name'
:
'Panama'
}
|
1
2
3
4
5
6
|
In [
27
]:
for
child
in
root:
...:
print
(child.tag, child.get(
'name'
))
...:
country Liechtenstein
country Singapore
country Panama
|
1
2
3
4
5
|
In [
21
]: root.getchildren()
Out[
21
]:
[
'country'
at
0x7f673581c728
>,
'country'
at
0x7f673581ca98
>,
'country'
at
0x7f673581cc28
>]
|
1
2
3
4
5
|
In [
9
]: root[
0
][
1
].text
Out[
9
]:
'2008'
In [
10
]: root[
1
][
0
].text
Out[
10
]:
'4'
|
1
2
|
In [
13
]: root.find(
'country'
).attrib
Out[
13
]: {
'name'
:
'Liechtenstein'
}
|
1
2
3
4
5
6
|
In [
16
]:
for
country
in
root.findall(
'country'
):
...:
print
(country.attrib)
...:
{
'name'
:
'Liechtenstein'
}
{
'name'
:
'Singapore'
}
{
'name'
:
'Panama'
}
|
1
2
|
In [
22
]: root.iterfind(
'country'
)
Out[
22
]:
object
prepare_child.<
locals
>.select at
0x7f6736dccfc0
>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
In [
19
]: root.findall(
'.//rank'
)
/
/
查找任意层次元素
Out[
19
]:
[
'rank'
at
0x7f673581c8b8
>,
'rank'
at
0x7f673581c6d8
>,
'rank'
at
0x7f673581cc78
>]
In [
32
]: root.findall(
'country/*'
)
/
/
查找孙子节点元素
Out[
32
]:
[
'rank'
at
0x7f673581c8b8
>,
'year'
at
0x7f673581cbd8
>,
'gdppc'
at
0x7f673581c958
>,
'neighbor'
at
0x7f673581c688
>,
'neighbor'
at
0x7f673581cb38
>,
'rank'
at
0x7f673581c6d8
>,
'year'
at
0x7f673581c5e8
>,
'gdppc'
at
0x7f673581c868
>,
'neighbor'
at
0x7f673581cb88
>,
'rank'
at
0x7f673581cc78
>,
'year'
at
0x7f673581ccc8
>,
'gdppc'
at
0x7f673581cd18
>,
'neighbor'
at
0x7f673581cd68
>,
'neighbor'
at
0x7f673581cdb8
>]
In [
33
]: root.findall(
'.//rank/..'
)
/
/
..表示父元素
Out[
33
]:
[
'country'
at
0x7f673581c728
>,
'country'
at
0x7f673581ca98
>,
'country'
at
0x7f673581cc28
>]
In [
34
]: root.findall(
'country[@name]'
)
/
/
包含name属性的country
Out[
34
]:
[
'country'
at
0x7f673581c728
>,
'country'
at
0x7f673581ca98
>,
'country'
at
0x7f673581cc28
>]
In [
35
]: root.findall(
'country[@name="Singapore"]'
)
/
/
name属性为Singapore的country
Out[
35
]: [
'country'
at
0x7f673581ca98
>]
In [
36
]: root.findall(
'country[rank]'
)
/
/
孩子元素中包含rank的country
Out[
36
]:
[
'country'
at
0x7f673581c728
>,
'country'
at
0x7f673581ca98
>,
'country'
at
0x7f673581cc28
>]
In [
37
]: root.findall(
'country[rank="68"]'
)
/
/
孩子元素中包含rank且rank元素的text为
68
的country
Out[
37
]: [
'country'
at
0x7f673581cc28
>]
In [
38
]: root.findall(
'country[1]'
)
/
/
第一个country
Out[
38
]: [
'country'
at
0x7f673581c728
>]
In [
39
]: root.findall(
'country[last()]'
)
/
/
最后一个country
Out[
39
]: [
'country'
at
0x7f673581cc28
>]
In [
40
]: root.findall(
'country[last()-1]'
)
/
/
倒数第二个country
Out[
40
]: [
'country'
at
0x7f673581ca98
>]
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
In [
29
]: root.
iter
()
Out[
29
]: <_elementtree._element_iterator at
0x7f67355dd728
>
In [
30
]:
list
(root.
iter
())
Out[
30
]:
[
'data'
at
0x7f673581c778
>,
'country'
at
0x7f673581c728
>,
'rank'
at
0x7f673581c8b8
>,
'year'
at
0x7f673581cbd8
>,
'gdppc'
at
0x7f673581c958
>,
'neighbor'
at
0x7f673581c688
>,
'neighbor'
at
0x7f673581cb38
>,
'country'
at
0x7f673581ca98
>,
'rank'
at
0x7f673581c6d8
>,
'year'
at
0x7f673581c5e8
>,
'gdppc'
at
0x7f673581c868
>,
'neighbor'
at
0x7f673581cb88
>,
'country'
at
0x7f673581cc28
>,
'rank'
at
0x7f673581cc78
>,
'year'
at
0x7f673581ccc8
>,
'gdppc'
at
0x7f673581cd18
>,
'neighbor'
at
0x7f673581cd68
>,
'neighbor'
at
0x7f673581cdb8
>]
In [
31
]:
list
(root.
iter
(
'rank'
))
Out[
31
]:
[
'rank'
at
0x7f673581c8b8
>,
'rank'
at
0x7f673581c6d8
>,
'rank'
at
0x7f673581cc78
>]
|