Is there any way to extract the encoding, like utf8, from a shapefile?

I want to be able to write a script that enables the user to import shapefiles without having to worry about which encoding the file. I'm using shp2pgsql but here I have to specify which encoding to import with...

I have a script that's written in SQL using gdal-tools. I would like to extend the script with either:

  1. somehow extracting the encoding of the shapefile to be able to use shp2pgsql to import or,

  2. is there some way to magically say "import with the encoding that is set in the file"?

I'm no programmer at heart, mearly faking it with SQL...


Assuming you are using PyQGIS, you can get it like this:

layer = iface.activeLayer()

Sample results with a few shapefiles I tested:


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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