Search code examples
pythonunicodeogr

Converting unicode fields into texts


I have a shapefile (.shp) with field names wriiten in Japanese language. I wanted to read the field names in Japanese language using the program below:

import ogr
infile = r"E:\shp\test.shp"
ds = ogr.Open(infile,0); slayer = ds.GetLayer(0)
fieldNames = [slayer.GetLayerDefn().GetFieldDefn(i).GetName() for i in range(0,slayer.GetLayerDefn().GetFieldCount())]

for x in fieldNames:
    print x

But, it printed out as follows, which is not readable.

ツwヘW
ツxヘW
’c’n–¼

How can I get readabe Japanese text for the field names?

I also tried as x.decode('utf8'), but got error message UnicodeDecodeError: 'utf8' codec can't decode byte 0x95 in position 0: invalid start byte

How to do it guys?


Solution

  • Not positive here, just trying to help, but maybe try casting the string as a unicode sequence like so?

    a = "Hello, World!"
    u = unicode(a,'utf-8')
    
    print type(a)
    print a
    print type(u)
    print u
    

    output:

    <type 'str'>
    Hello!
    <type 'unicode'>
    Hello!