小编Li *_*per的帖子

如何获得UnicodeDecodeError发生的位置?

如何获得UnicodeDecodeError发生位置的位置?我在这里找到了资料,并尝试在下面实现。但是我得到一个错误NameError: name 'err' is not defined

我已经在Internet上和StackOverflow上的所有位置进行了搜索,但是找不到任何提示来使用它。在python docs中,它说此特定异常具有start属性,因此它必须是可能的。

谢谢。

    data = buffer + data
    try:
        data = data.decode("utf-8")
    except UnicodeDecodeError:
        #identify where did the error occure?
        #chunk that piece off -> copy troubled piece into buffer and 
        #decode the good one -> then go back, receive the next chunk of 
        #data and concatenate it to the buffer.

        buffer = err.data[err.start:]
        data = data[0:err.start]
        data = data.decode("utf-8")
Run Code Online (Sandbox Code Playgroud)

python exception-handling exception python-3.x

2
推荐指数
1
解决办法
418
查看次数

在python中定义函数时初始化变量?

学习Python,尝试阅读NASA程序。为什么定义函数时show=True?我们可以这样初始化变量吗?我看不出它有什么用处。

def visualizeDomain(domain, show=True):
    '''Draw all the sensors and ground truth from a domain'''
    centerMap(domain.center[0], domain.center[1], 11)
    for s in domain.sensor_list:
        apply(addToMap, s.visualize(show=show))
    if domain.ground_truth != None:
        addToMap(domain.ground_truth, {}, 'Ground Truth', False)
Run Code Online (Sandbox Code Playgroud)

感谢大家的帮助。

python variables initialization function

1
推荐指数
1
解决办法
4625
查看次数