Skip to content

Instantly share code, notes, and snippets.

@Xophe92
Created August 31, 2019 23:23
Show Gist options
  • Save Xophe92/ed2ab8d6382c4daa330848424e5d3049 to your computer and use it in GitHub Desktop.
Save Xophe92/ed2ab8d6382c4daa330848424e5d3049 to your computer and use it in GitHub Desktop.
playing with encodings
# coding: utf8
a = "Père Noël"
print(f"a = \"{a}\"")
print(f"type(a) => {type(a)} \n")
b = a.encode('utf-8')
print(f"b = {b}")
print(f"type(b) => {type(b)} \n")
print("{encoding} \t {b.decode(encoding)}")
for encoding in ['cp1252', 'utf-8']:
print(f"{encoding} \t\t {b.decode(encoding)}\n")
print("utf8 code sur 1 à 4 bytes de nombreux caractères")
c = b"\xe5\x81\x9a\xe6\x88\x8f\xe4\xb9\x8b\xe8\xaf\xb4"
print(c.decode('utf-8'))
"""
a = "Père Noël"
type(a) => <class 'str'>
b = b'P\xc3\xa8re No\xc3\xabl'
type(b) => <class 'bytes'>
{encoding} {b.decode(encoding)}
cp1252 Père Noël
utf-8 Père Noël
utf8 code sur 1 à 4 bytes de nombreux caractères
做戏之说
Process finished with exit code 0
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment