Skip to content

Instantly share code, notes, and snippets.

@hvwaldow
Last active January 20, 2017 14:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hvwaldow/b27f2399ada4e272dabe51cc68656c5c to your computer and use it in GitHub Desktop.
Save hvwaldow/b27f2399ada4e272dabe51cc68656c5c to your computer and use it in GitHub Desktop.
Test encoding issues with regard to Py2 / Py3 compatibility
# _*_ encoding: utf-8 _*_
import sys
pv = sys.version_info[0]
print("Python Version: {}".format(pv))
strngs = (('str_asci','asci'),
('uni_asci', u'asci'),
('byte_ascii', b'asci'),
('str_chin', '漢字'),
('uni_chin', u'漢字'),
('byte_chin', b'\xe6\xbc\xa2\xe5\xad\x97'),
('gibberish', b'\xff\xa6\xf5'))
print("Input:")
for k, v in strngs:
print(k, v, type(v))
print('')
print("Treatments Old")
for k, v in strngs:
try:
v_to = v.encode('utf-8')
except Exception as e:
print('ENCODING ERROR OLD: ', (k, v))
print(e)
else:
print(k, v_to, type(v_to))
print('')
print("Treatments New")
for k, v in strngs:
try:
if sys.version_info[0] > 2:
if type(v) == str:
v_to = v # type(s) is PY3<str>
else:
v_to = v.decode('utf-8') # type(s) is PY3<bytes> or illegal
elif type(v) == unicode:
v_to = v.encode('utf-8') # It is PY2 :class:unicode
else:
v_to = v.decode('utf-8').encode('utf-8') #type(s) is PY2<str> or illegal
print(k, v_to, type(v_to))
except Exception as e:
print('ENCODING ERROR NEW: ', (k, v))
print(e)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment