回答編集履歴
5
f
test
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
下記ではいかがでしょうか
|
2
2
|
|
3
|
-
※読み込むサンプルのxml
|
3
|
+
※読み込むサンプルのXMLファイル(sample.xml)
|
4
4
|
|
5
5
|
```xml
|
6
6
|
|
@@ -29,6 +29,8 @@
|
|
29
29
|
</Result>
|
30
30
|
|
31
31
|
```
|
32
|
+
|
33
|
+
|
32
34
|
|
33
35
|
コード(内包表記なし)
|
34
36
|
|
4
修正
test
CHANGED
@@ -1,4 +1,36 @@
|
|
1
1
|
下記ではいかがでしょうか
|
2
|
+
|
3
|
+
※読み込むサンプルのxml
|
4
|
+
|
5
|
+
```xml
|
6
|
+
|
7
|
+
<Result>
|
8
|
+
|
9
|
+
<SubCodes>
|
10
|
+
|
11
|
+
<SubCode code="item-code-red-s">
|
12
|
+
|
13
|
+
<Option name="カラー" value="red"/>
|
14
|
+
|
15
|
+
<Option name="サイズ" value="small"/>
|
16
|
+
|
17
|
+
</SubCode>
|
18
|
+
|
19
|
+
<SubCode code="item-code-red-m">
|
20
|
+
|
21
|
+
<Option name="カラー" value="red"/>
|
22
|
+
|
23
|
+
<Option name="サイズ" value="medium"/>
|
24
|
+
|
25
|
+
</SubCode>
|
26
|
+
|
27
|
+
</SubCodes>
|
28
|
+
|
29
|
+
</Result>
|
30
|
+
|
31
|
+
```
|
32
|
+
|
33
|
+
コード(内包表記なし)
|
2
34
|
|
3
35
|
```
|
4
36
|
|
@@ -20,26 +52,48 @@
|
|
20
52
|
|
21
53
|
|
22
54
|
|
23
|
-
result =
|
55
|
+
result_lst = []
|
24
56
|
|
25
57
|
|
26
58
|
|
59
|
+
for subcode in subcodes:
|
60
|
+
|
27
|
-
subcodes
|
61
|
+
subcode_lst=[]
|
62
|
+
|
63
|
+
for option in subcode:
|
64
|
+
|
65
|
+
option_lst=[]
|
66
|
+
|
67
|
+
for attr in option.keys():
|
68
|
+
|
69
|
+
if attr in ["value", "name"]:
|
70
|
+
|
71
|
+
option_lst.append(option.attrib[attr])
|
72
|
+
|
73
|
+
subcode_lst.append(':'.join(option_lst))
|
74
|
+
|
75
|
+
result_lst.append("#".join(subcode_lst)+"="+subcode.attrib["code"])
|
28
76
|
|
29
77
|
|
30
78
|
|
31
|
-
dic = {'subcodes
|
79
|
+
dic = {'subcodes':'|'.join(result_lst)}
|
32
80
|
|
33
|
-
'#'.join(':'.join(option.attrib[attr] for attr in option.keys() if attr in ["name", "value"])
|
34
81
|
|
35
|
-
for option in subcode) + "=" + subcode.attrib["code"]
|
36
82
|
|
37
|
-
|
83
|
+
print(dic)
|
38
84
|
|
39
85
|
|
40
86
|
|
41
87
|
|
42
88
|
|
89
|
+
```
|
90
|
+
|
91
|
+
|
92
|
+
|
43
|
-
|
93
|
+
出力結果
|
44
94
|
|
45
95
|
```
|
96
|
+
|
97
|
+
>> {'subcodes:': 'カラー:red#サイズ:small=item-code-red-s|カラー:red#サイズ:medium=item-code-red-m'}
|
98
|
+
|
99
|
+
```
|
3
修正
test
CHANGED
@@ -30,9 +30,9 @@
|
|
30
30
|
|
31
31
|
dic = {'subcodes:': ('|'.join(
|
32
32
|
|
33
|
-
'#'.join(':'.join(option.attrib[attr] for attr in option.keys())
|
33
|
+
'#'.join(':'.join(option.attrib[attr] for attr in option.keys() if attr in ["name", "value"])
|
34
34
|
|
35
|
-
|
35
|
+
for option in subcode) + "=" + subcode.attrib["code"]
|
36
36
|
|
37
37
|
for subcode in subcodes))}
|
38
38
|
|
2
修正
test
CHANGED
@@ -20,9 +20,17 @@
|
|
20
20
|
|
21
21
|
|
22
22
|
|
23
|
+
result = ET.fromstring(xml_data)
|
24
|
+
|
25
|
+
|
26
|
+
|
27
|
+
subcodes = result[0]
|
28
|
+
|
29
|
+
|
30
|
+
|
23
31
|
dic = {'subcodes:': ('|'.join(
|
24
32
|
|
25
|
-
'#'.join(
|
33
|
+
'#'.join(':'.join(option.attrib[attr] for attr in option.keys())
|
26
34
|
|
27
35
|
for option in subcode) + "=" + subcode.attrib["code"]
|
28
36
|
|
@@ -30,6 +38,8 @@
|
|
30
38
|
|
31
39
|
|
32
40
|
|
41
|
+
|
42
|
+
|
33
43
|
print(dic)
|
34
44
|
|
35
45
|
```
|
1
修正
test
CHANGED
@@ -2,13 +2,13 @@
|
|
2
2
|
|
3
3
|
```
|
4
4
|
|
5
|
+
import xml.etree.ElementTree as ET
|
6
|
+
|
7
|
+
|
8
|
+
|
5
9
|
with open("sample.xml", mode="r", encoding="utf-8") as f:
|
6
10
|
|
7
11
|
xml_data = f.read()
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
import xml.etree.ElementTree as ET
|
12
12
|
|
13
13
|
|
14
14
|
|
@@ -18,7 +18,9 @@
|
|
18
18
|
|
19
19
|
subcodes = result[0]
|
20
20
|
|
21
|
+
|
22
|
+
|
21
|
-
dic = {'subcodes':('|'.join(
|
23
|
+
dic = {'subcodes:': ('|'.join(
|
22
24
|
|
23
25
|
'#'.join(f"{option.attrib['name']}:{option.attrib['value']}"
|
24
26
|
|