utf-8 python code example

Example 1: python open encoding utf-8

from io import open
f = open("test", mode="r", encoding="utf-8")

Example 2: python utf8

# insert in the first line of the file

#!/usr/bin/python3
# -*- coding: utf-8 -*-

# or

#!/usr/bin/env python
# -*- coding: utf-8 -*-

Example 3: python Non-UTF-8 code starting with

# coding=utf-8

Example 4: convert \x unicode utf 8 bytes to \u python

>>> '\xc5\x81'.decode('utf-8')
u'\u0141'

Example 5: how to convert utf-16 file to utf-8 in python

with open(ff_name, 'rb') as source_file:
  with open(target_file_name, 'w+b') as dest_file:
    contents = source_file.read()
    dest_file.write(contents.decode('utf-16').encode('utf-8'))

Example 6: python print utf-8

# credit to the Stack Overflow user in the source link

TestText = "Test - āĀēĒčČ..šŠūŪžŽ" # this not UTF-8...it is a Unicode string in Python 3.X.
TestText2 = TestText.encode('utf8') # this is a UTF-8-encoded byte string.