
windowsでpythonの処理をしていると、このようなエラーが良くでます。
これはpythonでwindowsにファイルをエクスポートする際、UTF-8からShift_JISに変換されているのですが、この文字コードの変換に対応できない文字があった場合に処理できずにエラーになります。
そこで今回は文字列をエラー無視でエンコード・デコードする方法でエラーが起こらないようにします。
1 2 3 4 5 |
text = '\xa0' print(text) UnicodeEncodeError: 'cp932' codec can't encode character '\xa0' in position 0: illegal multibyte sequence |
このようにエラーになります。
1 2 3 4 5 6 7 |
text = '\xa0...' text = text.encode('cp932','ignore') text = text.decode('cp932') print(text) #... |
そこでこのようにエラーを無視してエンコード・デコードすることでエラーが出る文字列を取り除くことが出来ます。
