回答編集履歴
2
Update
answer
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
**BeautifulSoup**
|
1
2
|
```python
|
2
3
|
from bs4 import BeautifulSoup
|
3
4
|
|
@@ -16,4 +17,26 @@
|
|
16
17
|
['nc', 'Q6'],
|
17
18
|
['es', 'QJ7'],
|
18
19
|
:
|
20
|
+
```
|
21
|
+
|
22
|
+
**xml.etree.ElementTree**
|
23
|
+
```
|
24
|
+
import xml.etree.ElementTree as ET
|
25
|
+
|
26
|
+
with open('data.xml') as f:
|
27
|
+
root = ET.fromstring(f.read())
|
28
|
+
|
29
|
+
board = [[e.tag, e.text] for e in root.findall('board/*')]
|
30
|
+
|
31
|
+
from pprint import pprint
|
32
|
+
pprint(board)
|
33
|
+
|
34
|
+
#
|
35
|
+
[['num', '1'],
|
36
|
+
['ns', 'AKT53'],
|
37
|
+
['nh', 'K82'],
|
38
|
+
['nd', 'QT3'],
|
39
|
+
['nc', 'Q6'],
|
40
|
+
['es', 'QJ7'],
|
41
|
+
:
|
19
42
|
```
|
1
Update
answer
CHANGED
@@ -1,8 +1,9 @@
|
|
1
1
|
```python
|
2
2
|
from bs4 import BeautifulSoup
|
3
3
|
|
4
|
+
with open('data.xml') as f:
|
4
|
-
soup = BeautifulSoup(
|
5
|
+
soup = BeautifulSoup(f, 'lxml')
|
5
|
-
board = [[e.name, e.text] for e in soup.select('board *')]
|
6
|
+
board = [[e.name, e.text] for e in soup.select('board *')]
|
6
7
|
|
7
8
|
from pprint import pprint
|
8
9
|
pprint(board)
|