ASCII编解码器无法解码字节0xc3

时间:2017-03-30 10:01:11

标签: python-2.7 encoding utf-8 ascii

我使用Python-2.7和PyShp来处理GIS数据(作为列表列表导入)。我开发了一个python脚本,它使用用户输入过滤掉数据作为过滤的标准。这适用于我用来开发脚本的文件。现在我想使用另一个文件,我收到错误:

UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 72: ordinal not in range(128)

如果我说得对,那是因为ASCII的编码错误。但是我的标题是“# - - coding:utf-8 - - ”。为什么它使用ASCII呢?因为它与其他文件一起使用,我假设有一种方法可以告诉python在导入过程中使用utf-8。我的导入行(使用PyShp)如下所示:

sf = shapefile.Reader(pfad)

“pfad”是pfad = input(“问题?”),所以用户输入。适用于其他文件。导致ASCII错误的原因是什么,它不使用utf-8?

编辑:以下是PyShp读取shapefile数据的原始代码:

def __init__(self, *args, **kwargs):
    self.shp = None
    self.shx = None
    self.dbf = None
    self.shapeName = "Not specified"
    self._offsets = []
    self.shpLength = None
    self.numRecords = None
    self.fields = []
    self.__dbfHdrLength = 0
    # See if a shapefile name was passed as an argument
    if len(args) > 0:
        if is_string(args[0]):
            self.load(args[0])
            return
    if "shp" in kwargs.keys():
        if hasattr(kwargs["shp"], "read"):
            self.shp = kwargs["shp"]
            if hasattr(self.shp, "seek"):
                self.shp.seek(0)
        if "shx" in kwargs.keys():
            if hasattr(kwargs["shx"], "read"):
                self.shx = kwargs["shx"]
                if hasattr(self.shx, "seek"):
                    self.shx.seek(0)
    if "dbf" in kwargs.keys():
        if hasattr(kwargs["dbf"], "read"):
            self.dbf = kwargs["dbf"]
            if hasattr(self.dbf, "seek"):
                self.dbf.seek(0)
    if self.shp or self.dbf:        
        self.load()
    else:
        raise ShapefileException("Shapefile Reader requires a shapefile or file-like object.")

编辑2:添加回溯。

Traceback (most recent call last):

File "C:\Users\Admin\Anaconda2\lib\site-packages\IPython\core\interactiveshell.py", line 2898, in run_code
self.showtraceback()

File "C:\Users\Admin\Anaconda2\lib\site-packages\IPython\core\interactiveshell.py", line 1824, in showtraceback
value, tb, tb_offset=tb_offset)

File "C:\Users\Admin\Anaconda2\lib\site-packages\IPython\core\ultratb.py", line 1406, in structured_traceback
self, etype, value, tb, tb_offset, number_of_lines_of_context)

File "C:\Users\Admin\Anaconda2\lib\site-packages\IPython\core\ultratb.py", line 1323, in structured_traceback
self, etype, value, elist, tb_offset, number_of_lines_of_context

File "C:\Users\Admin\Anaconda2\lib\site-packages\IPython\core\ultratb.py", line 644, in structured_traceback
out_list.extend(self._format_list(elist))

File "C:\Users\Admin\Anaconda2\lib\site-packages\IPython\core\ultratb.py", line 694, in _format_list
Colors.Normal)

UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 72: ordinal not in range(128)

0 个答案:

没有答案