How to Convert Non-ASCII Characters in Python

Written by alexander gokhfeld | 13/05/2017
How to Convert Non-ASCII Characters in Python
Python language represents non-ASCII characters by using Unicode. (Jupiterimages/ Images)

The American Standard Code for Information Interchange, or ASCII, encoding defines codes for English characters, punctuation marks and special symbols with the numeric values running from 0 to 127. When you want to read or store non-ASCII characters, such as characters in foreign alphabets or accented English letters, in the Python programming language, you must convert them to the closest visual equivalents of ASCII characters in Python supported encoding. By using Python's unicode function you can present non-ASCII characters in user applications.

Click "Start" in Windows and then click "Notepad" to open Notepad text editor.

Add the following code in the open window:

import sys


raw = 'A'.decode('cp437')

print raw


txt = unicode('A', encoding='utf-8')

print u'\00C1'.encode('cp437')

print txt

Click "File" and then click "Save.' Enter "" in the "File Name" box and click the "Save' button.

Click "Start" in Windows, select "Python 2.7" and then click "IDLE (Python GUI)." Click "File", select "Open" and then double-click on the "" in the open window.

Click "Run" and then click "Run Module" to decode non-ASCII "A" character in the "CP437" encoding to the "UTF-8" encoding.

Things you need

  • Python IDLE 2.7.1

Show MoreHide

By using the site, you consent to the use of cookies. For more information, please see our Cookie policy.