DISCOVER
×

How to Convert Non-ASCII Characters in Python

Updated April 17, 2017

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

sys.getdefaultencoding()

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

print raw

u=u'Á'

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

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

print txt

Click "File" and then click "Save.' Enter "nonascii.py" 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 "nonascii.py" 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'll Need

  • Python IDLE 2.7.1
Cite this Article A tool to create a citation to reference this article Cite this Article

About the Author

Alexander Gokhfeld has been writing since 2002. He has written for the monthly business publications "AutoPublish" and "MGRealty." Gokhfeld holds certification in business intelligence infrastructure development as well as a Bachelor of Science in electric engineering from Urban Trans College.