python编程遇见的异常

时间:2022-05-06 22:25:15
 import sys
 print('目前系统的编码为:',sys.getdefaultencoding()) # 目前系统的编码为: utf-8
 name = 'this is a test!'
 print(type(name))   # <class 'str'>
 print(name.encode("utf8"))  # encode()默认不写,是utf8编码
 print(name.decode("utf8"))  # 报错 AttributeError: 'str' object has no attribute 'decode'

报错代码

异常:AttributeError: 'str' object has no attribute 'decode'
原因:字符串是不能解码的,只有先对字符串编码才能解码,python默认的编码格式是unicode,字符串已经是unicode编码了,不需要decode

Python 3.* 用urllib.request来代替原来的urllib2