回答編集履歴
1
文字化け対策
test
CHANGED
@@ -10,9 +10,11 @@
|
|
10
10
|
|
11
11
|
|
12
12
|
|
13
|
-
df = pd.DataFrame({'
|
13
|
+
df = pd.DataFrame({'列1':[1,2], '列2':[3,4]}, index = ['index1','index2']) # 日本語を含む
|
14
14
|
|
15
15
|
df2 = df.to_html()
|
16
|
+
|
17
|
+
|
16
18
|
|
17
19
|
table = lxml.html.fromstring(df2)
|
18
20
|
|
@@ -20,15 +22,17 @@
|
|
20
22
|
|
21
23
|
# rootにdivを配置し、配下にtableとp(コメント)を配置
|
22
24
|
|
25
|
+
# 文字化けしないようにfromstringではunicode文字列で渡し、tostringではエンコーディングを指定する
|
26
|
+
|
23
|
-
root = lxml.html.fromstring(
|
27
|
+
root = lxml.html.fromstring(u'<div></div>')
|
24
28
|
|
25
29
|
root.append( table)
|
26
30
|
|
27
|
-
comment = lxml.html.fromstring('<p>
|
31
|
+
comment = lxml.html.fromstring(u'<p>コメント</p>')
|
28
32
|
|
29
33
|
root.append(comment)
|
30
34
|
|
31
|
-
html = lxml.html.tostring(root)
|
35
|
+
html = lxml.html.tostring(root,encoding='utf-8')
|
32
36
|
|
33
37
|
print(html)
|
34
38
|
|
@@ -46,9 +50,9 @@
|
|
46
50
|
|
47
51
|
<th></th>
|
48
52
|
|
49
|
-
<th>
|
53
|
+
<th>列1</th>
|
50
54
|
|
51
|
-
<th>
|
55
|
+
<th>列2</th>
|
52
56
|
|
53
57
|
</tr>
|
54
58
|
|
@@ -78,7 +82,7 @@
|
|
78
82
|
|
79
83
|
</tbody>
|
80
84
|
|
81
|
-
</table><p>
|
85
|
+
</table><p>コメント</p></div>
|
82
86
|
|
83
87
|
```
|
84
88
|
|