Python Developer's Guide to Character Encoding (2024)

Character encoding is a common problem in software development. Like other programming languages, character encoding in Python can be troublesome. In this article, we will dive deep into character encoding, discuss ways to interact with text and bytes in your Python 3 project, and fix common encoding errors using character encoding in Python 3.

What is Character Encoding?

In human language, text files on a computer contain a bunch of characters made of text or sentences, which could include English text,"a”, or Latin text,”ā”.In computer language, however, this text file contains bits and bytes, not text. Like English or Latin, computer stores characters as bytes. These bytes are more likecomputer codes, which are translated into human-readable text. This translation is character encoding.

Character encodingis a set of methods for mapping raw binary (0101110110) to readable characters (text) using an encoding lookup table. Every character is assigned a unique ID number, which helps computers read and understand text. Multiple types of character encodings are used for interpreting bytes. Often, the wrong character encoding is applied when interpreting bytes, causing them to display as strange-looking characters, such asvoil├Ā ‡å-ãor an unknown character, such as ������; even worse, it could cause an error that crashes your program.

While working with characters in Python, you will encounter two main data types: strings and bytes. Character encoding revolves around encoding and decoding these data types.

String Module

Strings are computer bytes interpreted and displayed in human-readable form. Before the development of Python 3, strings used the binary format by default to store bytes. It implies that strings are bytes withASCII set as the default encoding. While ASCII has been quite useful, it can only encode English language characters. Solving this problem led to the development of Universal Standard Encoding,Unicode. Unicode is a universal character set that defines all characters for different human languages used on computers with over one million code points. These code points refer to displayed text or characters.

In Python 3, every string uses the Unicode format to represent characters by default. It implies that each text has a specific code point that displays the characters usingUTF-8as the default encoding.

Bytes

The second data type isbytes, which are a series of integers. A byte is a collection of eight bits that represent a unit of information. Characters in a computer include text or strings made up of one or morebytes. Imagine that you receive a file containing data in bytes. You will need to translate these bytes into readable text. In this case, character encoding converts the character you select into the correct bytes in the computer’s memory before reading the bytes back into characters to show the text.

Working with Character Encoding in Python

As stated previously, Python 3 has two data types: strings and bytes. The process of moving between strings and bytes is known as encoding and decoding in Python. Now, let us dive into how this works.

Encoding Strings into Bytes

The process of converting strings (text) to computer bytes is known asencoding.In Python 3, strings represent human-readable text, which is Unicode characters. Unicode is not encoding but an abstract encoding standard that uses Unicode Transformation Format (UTF) for encoding. Although there are multiple character encodings, we will be working withUTF-8,which is the default encoding for Python 3. UTF refers to the standard Python encoding, and8refers to the 8-bit units used in character encoding. UTF-8 is a standard and efficient encoding of Unicode strings that represents characters in one-, two-, three-, or four-byte units. Python uses UTF-8 by default, which means it does not need to be specified in every Python file.

To encode a string into bytes, add the encode method, which will return the binary representation of the string.

>>> text = 'Hello World'>>> text.encode('utf-8')b'Hello Word'

The output is the binary representation of Hello. Next, let’s look at something more complex.

>>> text = 'parlé'>>> text.encode('utf-8')b'parl\xc3\xa9'>>> text = 'résumé'>>> text = text.encode('utf-8')b'r\xc3\xa9sum\xc3\xa9'

In the code above, the letters 'parl' are ASCII characters, which allows them to be represented. Each character in the ASCII table represents a single byte. However, complicated characters such as é are not ASCII-compatible in UTF-8 and are represented by two bytes encoded, xc3 and xa9, as in the first example.In the second example, strings that are not ASCII-compatible are represented by three bytes encoded. UTF-8 can go up to four bytes encoded. It means complicated characters in UTF-8 require several bytes for their binary representation.

Decoding Bytes into Strings

Converting a bytes-object to a string is known asdecoding. To decode bytes into strings, call the decode() method and specify the type of character encoding you wish to use. Of course, we are using UTF-8.

>>> text = b'parl\xc3\xa9'>>> text.decode('utf-8')'parlé'>>> text = b'r\xc3\xa9sum\xc3\xa9'>>> text.decode('utf-8)'résumé'

When we pass a binary format along with the decode method, the output is our original string.

  • Remember, you do not have to specify UTF-8 when working with Python 3. We are only specifying it here to show the encoding used.

Reading a Text File in Python

A file on a computer does not contain readable text. To read the characters in the file as text, you need to use the read() method. In addition, in Python, when you open a file for reading or writing, it is best practice to state the character encoding with which you are working. This is because, when working with text files, Python uses different character encodings depending on the operating system by default. Usually, when you open a file using the open() method, Python automatically treats it as a text file to convert the bytes in the text file to a string with the encoding you want. It is best to specify the encoding.

Here is an example:

>>> with open("data.txt", mode="r", encoding="utf-8") as f:... message = f.read()

Writing a Text File in Python

You can also use the open() method to write files. To write, set it to write by entering mode = w:

>>> with open("data.txt", mode="w", encoding="utf-8") as f:... f.write("Hi, I am having fun learning Python")

Other Encodings Available in Python

As mentioned previously, there are multiple encodings available for Unicode characters in Python. We already discussed UTF-8, which is the most common and widely used. It is also the default encoding for Python 3, but there are others:

UTF-16: UTF-16 encoding for Unicode characters represents characters in two or four bytes. The lowest binary representation of a character in UTF-16 consists of two bytes. The major advantage UTF-8 has over UTF-16 is that while the former uses one byte for encoding an ASCII character, the latter encodes the same character with two or more bytes. A UTF-16-encoded English text file is at least twice as large as a UTF-8-encoded version of the same file.

UTF-32: UTF-32 uses fixed four bytes for encoding Unicode characters. This means that every character encoding uses four bytes. UTF-16 uses more memory compared to UTF-8 and UTF-16. It is faster and preferable for string manipulation because you can calculate the length of a string in bytes using the number of characters in the string. However, for every ASCII character, you use an additional three bytes.

Pitfalls and How to Fix Them

Avoid character encoding errors at all cost, as they are troublesome and no developer enjoys spending time dealing with a bug. Let us look at common pitfalls in character encoding and how to fix likely errors.One common error raised when working with character encoding in Python 3 is the UnicodeEncodeError. There are a few causes of this error.

First, a UnicodeEncodeError error may occur when using characters that cannot be encoded, such as emojis. Unicode supports the vast majority of languages, but not all. Therefore, a character not supported in Unicode will fail. Second, when the strict method is used by default for encoding and decoding, it will cause an error if a character cannot be encoded or decoded.

There is also a UnicodeDecodeError, which occurs when the character encoding of the bytes we are reading and the character encoding Python is attempting to use to read them are not similar.

How Can I Avoid or Fix This Error?

One way to fix a character encoding error is to use the ignore or replace method to remove special characters or emojis that cannot be encoded. You can also use the ignore method when opening a file to avoid any errors. Here is an example:

text = 'ф'with open('message.txt', 'w', encoding='utf-8', errors='ignore') as f: f.write(text)

Conclusion

Computers do not recognize text; they store data in binary format. Character encoding is the key that converts this binary data to readable text. In this article, we have discussed several topics.The early method of character encoding, ASCII, was insufficient as it did not allow non-English characters to be represented in binary format. This was resolved by the introduction of Unicode, which assigned a specific code point for every human-readable character. We also discussed how encoding works in Python 3 and the various character encoding methods in Python.

I hope this tutorial was helpful to you.Happy Coding! 🙂

Python Developer's Guide to Character Encoding (2024)
Top Articles
Best Fixed Deposit Rates in Singapore (August 2024)
Send Money to India from the United States with Remitly
The Tribes and Castes of the Central Provinces of India, Volume 3
Chs.mywork
Ups Stores Near
Here's how eating according to your blood type could help you keep healthy
Carter Joseph Hopf
Bbc 5Live Schedule
Florida (FL) Powerball - Winning Numbers & Results
Oscar Nominated Brings Winning Profile to the Kentucky Turf Cup
George The Animal Steele Gif
Otterbrook Goldens
Jenn Pellegrino Photos
Bend Pets Craigslist
Cambridge Assessor Database
How pharmacies can help
Air Force Chief Results
The Menu Showtimes Near Regal Edwards Ontario Mountain Village
Aris Rachevsky Harvard
Where Is George The Pet Collector
Pecos Valley Sunland Park Menu
Busted Mcpherson Newspaper
Gazette Obituary Colorado Springs
Understanding Gestalt Principles: Definition and Examples
Jordan Poyer Wiki
Timeline of the September 11 Attacks
Wat is een hickmann?
Rek Funerals
1979 Ford F350 For Sale Craigslist
11526 Lake Ave Cleveland Oh 44102
Southtown 101 Menu
5 Star Rated Nail Salons Near Me
Rubmaps H
Martin Village Stm 16 & Imax
Garrison Blacksmith's Bench
Police Academy Butler Tech
Space Marine 2 Error Code 4: Connection Lost [Solved]
That1Iggirl Mega
Culver's of Whitewater, WI - W Main St
Fapello.clm
Craigslist Farm And Garden Reading Pa
Costco Gas Foster City
Panolian Batesville Ms Obituaries 2022
Blow Dry Bar Boynton Beach
Menu Forest Lake – The Grillium Restaurant
Gt500 Forums
Greatpeople.me Login Schedule
Coleman Funeral Home Olive Branch Ms Obituaries
Jesus Calling Oct 6
Ok-Selection9999
Latest Posts
Article information

Author: Melvina Ondricka

Last Updated:

Views: 6680

Rating: 4.8 / 5 (68 voted)

Reviews: 91% of readers found this page helpful

Author information

Name: Melvina Ondricka

Birthday: 2000-12-23

Address: Suite 382 139 Shaniqua Locks, Paulaborough, UT 90498

Phone: +636383657021

Job: Dynamic Government Specialist

Hobby: Kite flying, Watching movies, Knitting, Model building, Reading, Wood carving, Paintball

Introduction: My name is Melvina Ondricka, I am a helpful, fancy, friendly, innocent, outstanding, courageous, thoughtful person who loves writing and wants to share my knowledge and understanding with you.