3

I have text with French accent in a DXF I import in QGIS3.16. The DXF is encoded in UTF-8 but as a result, the features attributes have encoding problems. The é charcacter are written é. Here's a portion of the code used:

# -*- coding: utf-8 -*-
layer = QgsVectorLayer(dxf_path + "|layername=entities|geometrytype=Point", "dxf_linestring", "ogr")
layer.setProviderEncoding(u'UTF-8')
QgsProject.instance().addMapLayer(layer)

When I convert the DXF from UTF-8 to ANSI in Notepad++, the problem is solved but I would prefer to convert in the script rather than converting the whole file.

1
  • What about changing encoding in layer properties on the source tab?
    – Zoltan
    Commented Dec 27, 2020 at 12:22

1 Answer 1

0

Maybe I didn't find a working method because the DXF dataprovider has not the capability.

Instead, I used a file encoding convert trick:

with open(file_utf8, 'rb') as source_file:
  with open(file_latin1, 'w+b') as dest_file:
    contents = source_file.read()
    dest_file.write(contents.decode('utf-8').encode('latin1'))

And it worked perfectly for my plugin.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.