LoginSignup
0

More than 5 years have passed since last update.

Python > try, except > UnicodeError 実装例

Last updated at Posted at 2018-04-10
動作環境
ideone (Python 3.5)

@ Scipy lecture notes, Edition 2017.1
https://www.scipy-lectures.org/_downloads/ScipyLectures-simple.pdf
p45

UnicodeErrorのエラー補足例がある。

参考にして実装してみた。

def filter_name(name):
    try:
        name = name.encode('ascii')
    except UnicodeError as e:
        raise e
    return name

res = filter_name('7of9')
print(res)
res = filter_name('7之9')
print(res)
stdout
7of9
stderr
Traceback (most recent call last):
  File "prog.py", line 10, in <module>
  File "prog.py", line 5, in filter_name
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe5 in position 0: ordinal not in range(128)

link

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0