回答編集履歴

2

Update

2021/11/07 03:38

投稿

melian
melian

スコア20655

test CHANGED
@@ -1,3 +1,5 @@
1
+ **BeautifulSoup**
2
+
1
3
  ```python
2
4
 
3
5
  from bs4 import BeautifulSoup
@@ -35,3 +37,47 @@
35
37
  :
36
38
 
37
39
  ```
40
+
41
+
42
+
43
+ **xml.etree.ElementTree**
44
+
45
+ ```
46
+
47
+ import xml.etree.ElementTree as ET
48
+
49
+
50
+
51
+ with open('data.xml') as f:
52
+
53
+ root = ET.fromstring(f.read())
54
+
55
+
56
+
57
+ board = [[e.tag, e.text] for e in root.findall('board/*')]
58
+
59
+
60
+
61
+ from pprint import pprint
62
+
63
+ pprint(board)
64
+
65
+
66
+
67
+ #
68
+
69
+ [['num', '1'],
70
+
71
+ ['ns', 'AKT53'],
72
+
73
+ ['nh', 'K82'],
74
+
75
+ ['nd', 'QT3'],
76
+
77
+ ['nc', 'Q6'],
78
+
79
+ ['es', 'QJ7'],
80
+
81
+ :
82
+
83
+ ```

1

Update

2021/11/07 03:38

投稿

melian
melian

スコア20655

test CHANGED
@@ -4,9 +4,11 @@
4
4
 
5
5
 
6
6
 
7
- soup = BeautifulSoup(open('data.xml'), 'lxml')
7
+ with open('data.xml') as f:
8
8
 
9
+ soup = BeautifulSoup(f, 'lxml')
10
+
9
- board = [[e.name, e.text] for e in soup.select('board *')]
11
+ board = [[e.name, e.text] for e in soup.select('board *')]
10
12
 
11
13
 
12
14