Site icon Roel Peters

Solving “CommandError: Unable to serialize database: ‘charmap’ codec can’t encode character…”

I’ve been working in Django a lot lately and needed to dump my database using Django’s dumpdata command. However, that produced an error. Here’s how I solved it.

django-admin dumpdata <app>.<object> -o db_dump_<suffix>.json

However, halfway the dump, my terminal returned an error.

CommandError: Unable to serialize database: 'charmap' codec can't encode character '\ufeff' in position 110: character maps to <undefined>

Apparently, there was a Byte Order Mark or BOM somewhere in my SQLite database, which uses UTF-8 encoding by default. 😒 However, Python, in the terminal, cannot process it, since it uses Unicode by default.

The solution? Force Python to use UTF-8, as follows:

python -Xutf8 manage.py dumpdata <app>.<object> -o db_dump_<suffix>.json

Now, your dump will be properly processed and stored as a JSON file.

Exit mobile version