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

错误分析:

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',因为字典没有这个方法呀

 In [1]: a = {"uid":"","communityid":"","cityid":""}              

 In [2]: type(a)
Out[2]: dict In [3]: a = a.encode("utf-8")
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-3-e0edb4553e35> in <module>
----> 1 a = a.encode("utf-8") AttributeError: 'dict' object has no attribute 'encode'

解决的办法:把a这个dict对象转换为字符类型

通过第11行代码,可以看到,a可以encode成功了,b开头的字符串表示bytes类型

 In [4]: a = str(a)                                                              

 In [5]: type(a)
Out[5]: str In [6]: a
Out[6]: "{'uid': '5171979', 'communityid': '338855', 'cityid': '16'}"  In [11]: b=a.encode("utf-8")                                                                                                                   10 In [12]: b                                                                                                                                    
 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方法

 In [8]: dir(dict)
Out[8]:
['__class__',
'__contains__',
'__delattr__',
'__delitem__',
'__dir__',
'__doc__',
'__eq__',
'__format__',
'__ge__',
'__getattribute__',
'__getitem__',
'__gt__',
'__hash__',
'__init__',
'__init_subclass__',
'__iter__',
'__le__',
'__len__',
'__lt__',
'__ne__',
'__new__',
'__reduce__',
'__reduce_ex__',
'__repr__',
'__setattr__',
'__setitem__',
'__sizeof__',
'__str__',
'__subclasshook__',
'clear',
'copy',
'fromkeys',
'get',
'items',
'keys',
'pop',
'popitem',
'setdefault',
'update',
'values']

使用dir查看str类型的所有属性,可以看第40行encode属性

 In [9]: dir(str)
Out[9]:
['__add__',
'__class__',
'__contains__',
'__delattr__',
'__dir__',
'__doc__',
'__eq__',
'__format__',
'__ge__',
'__getattribute__',
'__getitem__',
'__getnewargs__',
'__gt__',
'__hash__',
'__init__',
'__init_subclass__',
'__iter__',
'__le__',
'__len__',
'__lt__',
'__mod__',
'__mul__',
'__ne__',
'__new__',
'__reduce__',
'__reduce_ex__',
'__repr__',
'__rmod__',
'__rmul__',
'__setattr__',
'__sizeof__',
'__str__',
'__subclasshook__',
'capitalize',
'casefold',
'center',
'count',
'encode',
'endswith',
'expandtabs',
'find',
'format',
'format_map',
'index',
'isalnum',
'isalpha',
'isdecimal',
'isdigit',
'isidentifier',
'islower',
'isnumeric',
'isprintable',
'isspace',
'istitle',
'isupper',
'join',
'ljust',
'lower',
'lstrip',
'maketrans',
'partition',
'replace',
'rfind',
'rindex',
'rjust',
'rpartition',
'rsplit',
'rstrip',
'split',
'splitlines',
'startswith',
'strip',
'swapcase',
'title',
'translate',
'upper',
'zfill']

使用dir查看bytes类型的所有属性,可以看第39行decode属性

 In [14]: dir(bytes)
Out[14]:
['__add__',
'__class__',
'__contains__',
'__delattr__',
'__dir__',
'__doc__',
'__eq__',
'__format__',
'__ge__',
'__getattribute__',
'__getitem__',
'__getnewargs__',
'__gt__',
'__hash__',
'__init__',
'__init_subclass__',
'__iter__',
'__le__',
'__len__',
'__lt__',
'__mod__',
'__mul__',
'__ne__',
'__new__',
'__reduce__',
'__reduce_ex__',
'__repr__',
'__rmod__',
'__rmul__',
'__setattr__',
'__sizeof__',
'__str__',
'__subclasshook__',
'capitalize',
'center',
'count',
'decode',
'endswith',
'expandtabs',
'find',
'fromhex',
'hex',
'index',
'isalnum',
'isalpha',
'isdigit',
'islower',
'isspace',
'istitle',
'isupper',
'join',
'ljust',
'lower',
'lstrip',
'maketrans',
'partition',
'replace',
'rfind',
'rindex',
'rjust',
'rpartition',
'rsplit',
'rstrip',
'split',
'splitlines',
'startswith',
'strip',
'swapcase',
'title',
'translate',
'upper',
'zfill']

最新文章

  1. initWithCoder: 与initWithFrame:的区别
  2. Node.js 事件循环
  3. Python基础1-Python环境搭建
  4. [Javascript] Limit Built Branches on Travis
  5. 服务器能访问共享,但是ping不通解决方案
  6. C#/.net给textbox添加回车事件
  7. DataTable转List&lt;Model&gt;通用类【实体转换辅助类】
  8. 【转】安装Intel HAXM为Android 模拟器加速,30秒内启动完成
  9. Esxi 5.0下配置LSI SAS RAID卡
  10. (原)Ubuntu16中安装cuda toolkit
  11. 图解UML类与类之间的六中关系
  12. How to create a jump server in AWS VPC
  13. UWP中实现大爆炸效果(二)
  14. bzoj3527: [Zjoi2014]力 卷积+FFT
  15. Java Web之JSTL标准标签库总结
  16. Urozero Autumn 2016. UKIEPC 2016
  17. CF1038E Maximum Matching 搜索/区间DP
  18. JavaScript中如何检测一个变量是一个String类型?请写出函数实现
  19. ASP.NET SingalR + MongoDB 实现简单聊天室(一):搭建基本框架
  20. 镜像上传和Dockerfile

热门文章

  1. inexact rename detection was skipped due to too many files
  2. Android控件之HorizontalScrollView 去掉滚动条
  3. OpenMediaVault 系统安装问题
  4. 【HDU 1599】 Find the mincost route
  5. c#截图工具
  6. 解决UnicodeEncodeError: ‘ascii’ codec can’t encode characters in position xxx ordinal not in range(128)
  7. python中coding:utf-8和setdefaultencoding区别
  8. Math对象常用方法(取整细节)
  9. bzoj 1306: [CQOI2009]match循环赛【dfs+剪枝】
  10. P4357 [CQOI2016]K远点对(KDTree)