LoginSignup
2
2

More than 3 years have passed since last update.

パーセントエンコーディングをデコードする

Last updated at Posted at 2019-07-30

パーセントエンコーディングをデコードする

Python 2 ではサラッとできるのに、Python 3 だと微妙に面倒くささ.

Python 2 版

>>> s = '%82%A0%82%A2%82%A4%82%A6%82%A8'
>>> import re
>>> pattern = re.compile('%(..)')
>>> t = pattern.sub(lambda m: chr(int(m.group(1), 16)), s)
>>> print t.decode('cp932')
あいうえお

Python 3 版

>>> s = b'%82%A0%82%A2%82%A4%82%A6%82%A8'
>>> import re
>>> pattern = re.compile(b'%(..)')
>>> t = pattern.sub(lambda m: bytes([int(m.group(1), 16)]), s)
>>> print(t.decode('cp932'))
あいうえお
2
2
4

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
2
2