Skip to main content

Have you ever seen this?

UnicodeEncodeError: 'XXXXX' codec can't encode character 'XXXXX' in position 15: ordinal ...

Probably more than once, right? :) After having spent too much time on finding the right codecs for files, I wrote BruteCodecChecker. BruteCodecChecker (MIT) opens a file in all codecs available in your environment and prints the results. It also works for byte objects.

If you work, like me, with a lot of text files, it will save you a lot of time.

Install it:

pip install BruteCodecChecker

Try it:

from BruteCodecChecker import CodecChecker

teststuff = b"""This is a test! 

Hi there!

A little test! """

testfilename = "test_utf8.tmp"

with open("test_utf8.tmp", mode="w", encoding="utf-8-sig") as f:

    f.write(teststuff.decode("utf-8-sig"))

codechecker = CodecChecker()

codechecker.try_open_file(testfilename, readlines=2).print_results(

    pause_after_interval=1, items_per_interval=10

)

codechecker.try_open_file(testfilename).print_results()

codechecker.try_convert_bytes(teststuff.decode("cp850").encode()).print_results(

    pause_after_interval=1, items_per_interval=10

)

Output


Codec               : palmos                                                       

Mode                : strict

Length              : 32

Converted           : 

Line: 0              This is a test! 

Line: 1                  Hi there!

Codec               : ptcp154                                                      

Mode                : strict

Length              : 32

Converted           : 

Line: 0              п»ҝThis is a test! 

Line: 1                  Hi there!

Codec               : punycode                                                     

Mode                : strict

Codec               : quopri_codec                                                 

Mode                : strict

Codec               : raw_unicode_escape                                           

Mode                : strict

Length              : 32

Converted           : 

Line: 0              This is a test! 

Line: 1                  Hi there!

Codec               : rot_13                                                       

Mode                : strict

Codec               : shift_jis                                                    

Mode                : strict

Codec               : shift_jisx0213                                               

Mode                : strict

Length              : 31

Converted           : 

Line: 0              鬠ソThis is a test! 

Line: 1                  Hi there!

Codec               : shift_jis_2004                                               

Mode                : strict

Length              : 31

Converted           : 

Line: 0              鬠ソThis is a test! 

Line: 1                  Hi there!

Codec               : tis_620                                                      

Mode                : strict

Length              : 32

Converted           : 

Line: 0              ๏ปฟThis is a test! 

Line: 1                  Hi there!

Codec               : undefined                                                    

Mode                : strict

Codec               : unicode_escape                                               

Mode                : strict

Length              : 32

Converted           : 

Line: 0              This is a test! 

Line: 1                  Hi there!

Codec               : utf_16                                                       

Mode                : strict

Codec               : utf_16_be                                                    

Mode                : strict

Codec               : utf_16_le                                                    

Mode                : strict

Codec               : utf_32                                                       

Mode                : strict

Codec               : utf_32_be                                                    

Mode                : strict

Codec               : utf_32_le                                                    

Mode                : strict

Codec               : utf_7                                                        

Mode                : strict

Codec               : utf_8                                                        

Mode                : strict

Length              : 30

Converted           : 

Line: 0              This is a test! 

Line: 1                  Hi there!

Codec               : utf_8_sig                                                    

Mode                : strict

Length              : 29

Converted           : 

Line: 0              This is a test! 

Line: 1                  Hi there!

Metadata

Release files for BruteCodecChecker 0.21

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for BruteCodecChecker 0.21
File Size Uploaded
BruteCodecChecker-0.21.tar.gz 6.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for BruteCodecChecker 0.21
File Interpreter ABI Platform
BruteCodecChecker-0.21-py3-none-any.whl Python 3 none any Details

Total release size: 14.2 kB

Release files / BruteCodecChecker-0.21.tar.gz

Download URL BruteCodecChecker-0.21.tar.gz
Size 6.6 kB
Tags Source
SHA-256 checksum
How to use checksums
3023af65fbb433d525bcd4e5c93a87f34dafd2ffb035c7ebf9346e766baed2bc
BLAKE2b-256 checksum
How to use checksums
14e198fe9305f40b2982b8905d6c0cb9165ffe12d55e7dfbeedfa1ccb906d6e9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.9.13

Release files / BruteCodecChecker-0.21-py3-none-any.whl

Download URL BruteCodecChecker-0.21-py3-none-any.whl
Size 7.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7e25711794f0780c664d53d6eb619d7124d5a4ab26e4c18d999f4bbf13a447e4
BLAKE2b-256 checksum
How to use checksums
ddda67bc2c7b822ec783af442dde623c6b0daae1c0099b13bba4c8167b313e82
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.9.13

Release history Release notifications | RSS feed

This release

0.21 This release

2 release files

0.2

2 release files

0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page