Change Shapefile data source encoding?
From the menubar, go to:
Settings > Options > Data Sources > Data source handling
and uncheck the Ignore shapefile encoding declaration setting:
This sets all layers loaded into QGIS to be default encoded to UTF-8
To recapitulate the different location where you can set the encoding:
When you open a shapefile:
When you open a CSV:
When you save a vector file:
In the layer properties:
And, as mentioned by @Joseph, you should probably uncheck the Ignore shapefile encoding declaration setting.
When you use Save As ...
, you have to select a different filename, and Add file to canvas
.
As a consequence, you MUST save the project too to get the new shapefile in it. The unsaved project still includes the untouched shapefile, and the wrong encoding.
BTW the Data encoding
in the Layer properties is part of the project file, not the shapefile. Your code example does not write anything inside the shapefile, it changes just the layer settings of the project.