首先這是一個很簡單的 運行時錯誤:python
錯誤分析:git
AttributeError:屬性錯誤,形成這種錯誤的緣由可能有:api
對象是 None 的一個可能緣由,是你忘記從函數返回一個值;若是程序執行到函數
的末尾沒有碰到 return 語句,它就會返回 None 。另外一個常見的緣由是使用了列表
方法的結果,如 sort ,這種方法返回的是 Nonessh
個人緣由是1:我使用了dict.encode()方法,但實際上dict對象並沒encode方法。encode方法是屬於str對象的。ide
因而可知我對encode 和 decode不夠了解,對於調用他們的對象並不十分理解函數
encode編碼--調用這個方法的對象是str類型ui
decode解碼--調用這個方法的對象是bytes類型編碼
他們都是關於字符對象(str&bytes)的方法,因此像下面這樣,當a 是一個dict 字典類型的對象時,spa
調用encode()方法時就會報AttributeError: 'dict' object has no attribute 'encode',由於字典沒有這個方法呀 code
1 In [1]: a = {"uid":"5171979","communityid":"338855","cityid":"16"} 2 3 In [2]: type(a) 4 Out[2]: dict 5 6 In [3]: a = a.encode("utf-8") 7 --------------------------------------------------------------------------- 8 AttributeError Traceback (most recent call last) 9 <ipython-input-3-e0edb4553e35> in <module> 10 ----> 1 a = a.encode("utf-8") 11 12 AttributeError: 'dict' object has no attribute 'encode'
解決的辦法:把a這個dict對象轉換爲字符類型
經過第11行代碼,能夠看到,a能夠encode成功了,b開頭的字符串表示bytes類型
1 In [4]: a = str(a) 2 3 In [5]: type(a) 4 Out[5]: str 5 6 In [6]: a 7 Out[6]: "{'uid': '5171979', 'communityid': '338855', 'cityid': '16'}" 8 9 In [11]: b=a.encode("utf-8")
10 In [12]: b
11 Out[12]: b"{'uid': '5171979', 'communityid': '338855', 'cityid': '16'}"
In [13]: type(b)
Out[13]: bytes
In [13]: type(b)
Out[13]: bytes
附:
使用dir查看dict類型的全部屬性,能夠看到並無encode和decode方法
1 In [8]: dir(dict) 2 Out[8]: 3 ['__class__', 4 '__contains__', 5 '__delattr__', 6 '__delitem__', 7 '__dir__', 8 '__doc__', 9 '__eq__', 10 '__format__', 11 '__ge__', 12 '__getattribute__', 13 '__getitem__', 14 '__gt__', 15 '__hash__', 16 '__init__', 17 '__init_subclass__', 18 '__iter__', 19 '__le__', 20 '__len__', 21 '__lt__', 22 '__ne__', 23 '__new__', 24 '__reduce__', 25 '__reduce_ex__', 26 '__repr__', 27 '__setattr__', 28 '__setitem__', 29 '__sizeof__', 30 '__str__', 31 '__subclasshook__', 32 'clear', 33 'copy', 34 'fromkeys', 35 'get', 36 'items', 37 'keys', 38 'pop', 39 'popitem', 40 'setdefault', 41 'update', 42 'values']
使用dir查看str類型的全部屬性,能夠看第40行encode屬性
1 In [9]: dir(str) 2 Out[9]: 3 ['__add__', 4 '__class__', 5 '__contains__', 6 '__delattr__', 7 '__dir__', 8 '__doc__', 9 '__eq__', 10 '__format__', 11 '__ge__', 12 '__getattribute__', 13 '__getitem__', 14 '__getnewargs__', 15 '__gt__', 16 '__hash__', 17 '__init__', 18 '__init_subclass__', 19 '__iter__', 20 '__le__', 21 '__len__', 22 '__lt__', 23 '__mod__', 24 '__mul__', 25 '__ne__', 26 '__new__', 27 '__reduce__', 28 '__reduce_ex__', 29 '__repr__', 30 '__rmod__', 31 '__rmul__', 32 '__setattr__', 33 '__sizeof__', 34 '__str__', 35 '__subclasshook__', 36 'capitalize', 37 'casefold', 38 'center', 39 'count', 40 'encode', 41 'endswith', 42 'expandtabs', 43 'find', 44 'format', 45 'format_map', 46 'index', 47 'isalnum', 48 'isalpha', 49 'isdecimal', 50 'isdigit', 51 'isidentifier', 52 'islower', 53 'isnumeric', 54 'isprintable', 55 'isspace', 56 'istitle', 57 'isupper', 58 'join', 59 'ljust', 60 'lower', 61 'lstrip', 62 'maketrans', 63 'partition', 64 'replace', 65 'rfind', 66 'rindex', 67 'rjust', 68 'rpartition', 69 'rsplit', 70 'rstrip', 71 'split', 72 'splitlines', 73 'startswith', 74 'strip', 75 'swapcase', 76 'title', 77 'translate', 78 'upper', 79 'zfill']
使用dir查看bytes類型的全部屬性,能夠看第39行decode屬性
1 In [14]: dir(bytes) 2 Out[14]: 3 ['__add__', 4 '__class__', 5 '__contains__', 6 '__delattr__', 7 '__dir__', 8 '__doc__', 9 '__eq__', 10 '__format__', 11 '__ge__', 12 '__getattribute__', 13 '__getitem__', 14 '__getnewargs__', 15 '__gt__', 16 '__hash__', 17 '__init__', 18 '__init_subclass__', 19 '__iter__', 20 '__le__', 21 '__len__', 22 '__lt__', 23 '__mod__', 24 '__mul__', 25 '__ne__', 26 '__new__', 27 '__reduce__', 28 '__reduce_ex__', 29 '__repr__', 30 '__rmod__', 31 '__rmul__', 32 '__setattr__', 33 '__sizeof__', 34 '__str__', 35 '__subclasshook__', 36 'capitalize', 37 'center', 38 'count', 39 'decode', 40 'endswith', 41 'expandtabs', 42 'find', 43 'fromhex', 44 'hex', 45 'index', 46 'isalnum', 47 'isalpha', 48 'isdigit', 49 'islower', 50 'isspace', 51 'istitle', 52 'isupper', 53 'join', 54 'ljust', 55 'lower', 56 'lstrip', 57 'maketrans', 58 'partition', 59 'replace', 60 'rfind', 61 'rindex', 62 'rjust', 63 'rpartition', 64 'rsplit', 65 'rstrip', 66 'split', 67 'splitlines', 68 'startswith', 69 'strip', 70 'swapcase', 71 'title', 72 'translate', 73 'upper', 74 'zfill']