Adventures in Machine Learning

Removing Unicode in Python: Two Simple Solutions

Python is a programming language known for its readability, simplicity, and efficiency. It is not surprising that it has become a popular language in today’s tech industry.

Despite its strengths, however, one limitation of the Python language is dealing with Unicode characters.

One common issue programmers face is the presence of ufeff Unicode character, which is often accidently included when working in Python.

The character appears at the beginning of a file or string and is commonly referred to as “Byte Order Mark” or BOM. This article will discuss the two ways to remove the ufeff Unicode character in Python.

Removing ufeff Using str.replace()

The first method of removing ufeff Unicode character is by utilizing the str.replace() method. The str.replace() method is a commonly used Python string method that replaces all occurrences of a specified substring with another substring.

This method works by first calling the string that needs to be modified and chain the .replace() method afterward. The first argument of the .replace() method is the substring that needs to be replaced, while the second argument is the substring that will replace the first argument.

To remove ufeff BOM character from a string in Python using this method, we simply need to call the string where the BOM character is found and calls the .replace() method. The first argument of replace() method should be the BOM character represented by ufeff, and the second argument should be an empty string, i.e., ”.

Consider the following example of removing the ufeff character from a string using the str.replace() method:


text = ‘ufeffHello, World!’

text = text.replace(‘ufeff’, ”)



Output: Hello, World!

The above code works for a single string, but how about when opening a file?

Setting the encoding to utf-8-sig when opening a file

The second method of removing ufeff Unicode character is by ensuring that the right encoding is used when opening a file. This method is useful when dealing with files such as CSV or JSON, which may have Unicode characters present.

When a file is encoded in UTF-8, there are three different ways to represent the Unicode characters. One of these is known as the “UTF-8 BOM” or “utf-8-sig”.

This encoding is a Unicode Character Encoding Form that modifies UTF-8. It uses the byte order mark (BOM) as a signature for detecting the encoding form.

To ensure that the ufeff BOM character is removed automatically every time a file is opened in Python, we can add the utf-8-sig encoding option when opening a file. This can be done by passing the encoding parameter along with the file opening method.

Consider the following example:


with open(‘file.txt’, ‘r’, encoding=’utf-8-sig’) as file:

text =



By specifying utf-8-sig as the encoding type, we specify to Python to look for BOM characters and remove them automatically. The content of the file will then be read into the text variable, BOM-free.

Additional Resources

For more in-depth information on Unicode characters and their use in Python, tutorials are available online. Many online forums such as Stack Overflow are available for questions and immediate solutions.

Additionally, Python communities such as offer resources ideal for programmers of all skill levels.


In the article, we discussed the two common ways to remove the ufeff Unicode character in Python. From using the string.replace() method to setting the encoding to utf-8-sig while opening a file, programmers can easily deal with this common issue when working with Unicode characters.

With Python’s popularity and ability to work with various types of files, it is essential for programmers to understand how to handle various aspects of the language, including Unicode encoding. In summary, this article explored two ways to remove the ufeff Unicode character in Python: using the string.replace() method and setting the encoding to utf-8-sig while opening a file.

Both methods are quick, efficient, and provide a reliable solution when dealing with Unicode characters. Understanding how to handle Unicode encoding is crucial for Python programmers who work with various types of files.

By implementing the techniques highlighted in this article, programmers can streamline their coding process and improve their overall efficiency.

Popular Posts