问答详情
源自:4-2 python正则表达式练习

为什么加了decode('utf-8')还是出错

i = 0 for url in listurl:    f = open(str(i)+'.jpg','w')    req = urllib.request.urlopen(url)    buf = req.read()    buf = buf.decode('utf-8')    f.write(buf)    i+=1 出现UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte错误   求大神解答

提问者:jocundhang 2016-08-27 16:15

个回答

  • tracyT_mac
    2016-11-19 22:34:37

    decode(‘utf-8’, ‘ignore')
    指明ignore,忽略非法字符

  • jocundhang
    2016-08-27 16:33:29

    自己弄懂了 用f = open(str(i)+'.jpg','wb')就可以了