AttributeError: 'dict' object has no attribute 'encode'

首先这是一个很简单的 运行时错误

错误分析:

AttributeError:属性错误,造成这种错误的原因可能有:

  1. 你尝试访问一个不存在的属性或方法。检查一下拼写!你可以使用内建函数 dir 来列出存在的属性
  2. 如果一个属性错误表明一个对象是 NoneType ,那意味着它就是 None 。因此问题不在于属性名,而在于对象本身。

    对象是 None 的一个可能原因,是你忘记从函数返回一个值;如果程序执行到函数
的末尾没有碰到 return 语句,它就会返回 None 。另一个常见的原因是使用了列表
方法的结果,如 sort ,这种方法返回的是 None

我的原因是1:我使用了dict.encode()方法,但实际上dict对象并没encode方法。encode方法是属于str对象的。

由此可见我对encode 和 decode不够了解,对于调用他们的对象并不十分理解

encode编码--调用这个方法的对象是str类型

decode解码--调用这个方法的对象是bytes类型

他们都是关于字符对象(str&bytes)的方法,所以像下面这样,当a 是一个dict 字典类型的对象时,

调用encode()方法时就会报AttributeError: 'dict' object has no attribute 'encode',因为字典没有这个方法呀           

 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']