获取字段时出现DjangoUnicodeDecodeError

时间:2018-05-23 10:50:12

标签: python django geodjango

我在阅读GEOSGeometry对象时遇到了问题。我使用过这段代码

        ds = DataSource(shp file path)
lyr = ds[0]
 for feat in lyr:
                    geom_t = feat.geom.transform(wgs84, clone=True)
                    name =feat.get('name')

此代码适用于我的形状文件。但如果name字段包含utf8字符串,例如'تست',则会引发此错误

    DjangoUnicodeDecodeError at /views/importdata/
'utf-8' codec can't decode byte 0xc8 in position 0: invalid continuation byte. You passed in b'\xc8\xe1\xe6\xc7\xd1 \xc7\xe3\xc7\xe3 \xd1\xd6\xc7' (<class 'bytes'>)

Unicode error hint

The string that could not be encoded/decoded was: �����

我发现这是一个内部错误,与django中的gdal或geos包装有关。错误来自这一行

return force_text(string, encoding=self._feat.encoding, strings_only=True) 在此目录中的field.py中

D:\Python\Python36\lib\site-packages\django\contrib\gis\gdal\field.py in as_string

有没有办法找到解决这个问题的方法? 感谢

1 个答案:

答案 0 :(得分:0)

对于可能面临这个问题的其他人来说也是如此。我必须在数据源定义中设置encoding

这是主要部分

ds = DataSource(shp,encoding='cp1256')
相关问题