回答編集履歴

1

Pythonコード追加

2017/09/12 16:27

投稿

Tomak
Tomak

スコア1652

test CHANGED
@@ -10,7 +10,29 @@
10
10
 
11
11
 
12
12
 
13
- 普通はこのような制御文字コード(`[\x00-\x1f\x7f]`)は危険なので、削除してからMecabで解析してみてはいかがでしょうか。
13
+ 普通はこのような制御文字コード(`[\x00-\x1f\x7f]`)は危険なので、`reモジュール`の正規表現を使って削除してからMecabで解析してみてはいかがでしょうか。
14
+
15
+
16
+
17
+ ```python
18
+
19
+ #HTML文字列から制御コードを削除
20
+
21
+ text = re.sub(r'[\x00-\x1f\x7f]+', '', text)
22
+
23
+
24
+
25
+ #BOMを消すだけなら、下記のようにデコード→エンコードしてもOKです
26
+
27
+ text = text.decode('utf_8_sig').encode('utf-8')
28
+
29
+ ```
30
+
31
+
32
+
33
+ [http://docs.python.jp/3.6/library/re.html#re.sub](http://docs.python.jp/3.6/library/re.html#re.sub)
34
+
35
+ [http://docs.python.jp/3.6/library/stdtypes.html#str.encode](http://docs.python.jp/3.6/library/stdtypes.html#str.encode)
14
36
 
15
37
 
16
38