Skip to content

Instantly share code, notes, and snippets.

@FrostyX
Last active September 3, 2015 12:55
Show Gist options
  • Save FrostyX/d5cd704d85afb7fee455 to your computer and use it in GitHub Desktop.
Save FrostyX/d5cd704d85afb7fee455 to your computer and use it in GitHub Desktop.
Python unicode hell

Python unicode hell

Welcome to my personal piece of hell.

This repository is collection of commonly (by me) used snippets which I need to search over and over again, due to compatibility issues between Python2 and Python3. I hope it will make me stop trying killing myself by hitting the wall with my head.

References:

from __future__ import unicode_literals
import sys
PY3 = sys.version[0] == 3
# Working with user input
if not PY3:
input = raw_input
print(input()) # ěščřžýáíé
# File IO
x = "ěščřžýáíé"
with open("/tmp/pyunicode3", "wb") as f:
f.write(x)
with(open("/tmp/pyunicode", "r") as f:
print(f.read())
# Notice that just doing f.read() without print, in interpretter, the
# u'\u011b\u0161\u010d\u0159\u017e\xfd\xe1\xed\xe9' will be returned
from __future__ import unicode_literals
# PY2
x = "foo" # u'foo' | <type 'unicode'>
# PY3
x = "foo" # 'foo' | <type 'str'>
# ---
x = "ěščřžýáíé"
# PY2
# u'\u011b\u0161\u010d\u0159\u017e\xfd\xe1\xed\xe9'
# PY3
# 'ěščřžýáíé'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment