Python报错:‘str‘ object has no attribute ‘decode‘. Did you mean: ‘encode‘的解决办法?

一、解决办法(非常简单):

                在decode('utf8')前加.encode(),改为.encode().decode('utf8')
 

二、示例演示

报错代码
    for sample in cursor:
        sample = sample[0].decode('utf8')
        res += [sample]

修改后代码
for sample in cursor:
    sample = sample[0].encode().decode('utf8')
    res += [sample]


版权声明:本文为weixin_40810533原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。