Django import_export Imported file has a wrong encoding: 'charmap' codec can't decode byte

Viewed 502

I'm using django's import_export lib. But when I'm importing my json file I'm receiving the following error

Imported file has a wrong encoding: 'charmap' codec can't decode byte 0x81 in position 1602: character maps to

I've found similar questions but they don't conver django import_export.

My json file contains arabic caracters and ascii caracters. Here's a json object from it :

{"id":"35","code":"35","name_fr":"Boumerd\u00e8s","name_ar":"بومرداس"}

Here's my import_export class where I'm precising utf-8 encoding

class SpecAdmin(ImportExportActionModelAdmin):
from_encoding = 'utf-8'

def get_instance(self, instance_loader, row):
    try:
        params = {}
        for key in instance_loader.resource.get_import_id_fields():
            field = instance_loader.resource.fields[key]
            params[field.attribute] = field.clean(row)
        return self.get_queryset().get(**params)
    except Exception:
        return None

If anyone had a similar problem with this library can you please tell me what I'm doing wrong.

2 Answers

I tested the example characters you supplied and was able to import OK using the test app.

I would suggest you get the test app running for yourself and see if you can import the JSON file into it. To do this you could import one of the existing test files (e.g. books.csv), then export it as json. You can then add your own characters to the json and re-import to verify that it works ok.

pip install django-import-export==3.0.0b4

This will delete the previous version and install a new one. I made data export with the version of import export 2.8 from one project and received the same mistake when importing into the second project. After installing version 3.0.0.b4 in the second project, the import went without problems. (Export files were made with version 2.8, and imported with version 3.0.0.b4)

Related