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

错误分析:

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

AttributeError: 'dict' object has no attribute 'encode'的更多相关文章

  1. AttributeError: 'dict' object has no attribute 'has_key'

    运行下面的代码: if (locals().has_key('data')): del data gc.collect() 出错: if (locals().has_key('data')): Att ...

  2. AttributeError: 'dict' object has no attribute 'iteritems'

    在python3.6中运行 sortedClassCount = sorted(classCount.iteritems(), key=operator.itemgetter(1), reverse= ...

  3. flask 表单填充数据报错!AttributeError: 'dict' object has no attribute 'getlist'

    报错信息: AttributeError: 'dict' object has no attribute 'getlist' 解决: 虽然是小毛病,不得不说还是自己太粗心大意了.

  4. facenet pyhton3.5 训练 train_softmax.py 时报错AttributeError: 'dict' object has no attribute 'iteritems'

    报错原因:在进行facenet进行train_softmax.py训练时,在一轮训练结束进行验证时,报错AttributeError: 'dict' object has no attribute ' ...

  5. AttributeError: 'dict' object has no attribute 'status_code'

    前端AJAX请求数据,提示错误:“AttributeError: 'dict' object has no attribute 'status_code'”. 原因:是提示返回对象dict没有“sta ...

  6. 机器学习实战:KNN代码报错“AttributeError: 'dict' object has no attribute 'iteritems'”

    报错代码: sortedClassCount = sorted(classCount.iteritems(), key=operator.itemgetter(1), reverse=True) 解决 ...

  7. 'dict' object has no attribute 'a'

    a = {} #a.a = 'a' #AttributeError: 'dict' object has no attribute 'a' #a['a'] #KeyError: 'a' a['a'] ...

  8. python3 AttributeError: 'NoneType' object has no attribute 'split'

    from wsgiref.simple_server import make_server def RunServer(environ, start_response): start_response ...

  9. AttributeError: 'NoneType' object has no attribute 'split' 报错处理

    报错场景 social_django 组件对原生 django 的支持较好, 但是因为 在此DRF进行的验证为 JWT 方式 和 django 的验证存在区别, 因此需要进行更改自行支持 JWT 方式 ...

随机推荐

  1. bzoj2461: [BeiJing2011]符环

    再做水题就没救了 考虑DP...我们把正反面一起弄 fi,j,k,u表示第几个位置,正面多了多少左括号,背面多了多少没办法消的右括号,背面结尾的左括号数 #include<cstdio> ...

  2. I.MX6 RGB clock 和 data 重合

    /*********************************************************************** * I.MX6 RGB clock 和 data 重合 ...

  3. Spring Ioc容器核心类继承图

    Spring IOC容器其实就是BeanFactory的实例,Spring中BeanFactory的类关系结构如下图: 从上图可以看出Beanfactory作为根接口又细化出三个二级接口,最后又有Co ...

  4. python编写九九乘法表代码

    打印九九乘法表 代码: #!/usr/bin/env python # -*- coding: UTF-8 -*- # 项目二: # 1.要求:编写九九乘法表 # 2.分析: # 根据九九乘法表的样式 ...

  5. fiddler工具使用

    一.安装 a) 百度fiddler ,下载, 安装 ,无脑流 二.界面介绍 a) 工具栏与状态栏 其中保存是,可以为两种形式:1.text文本形式 2.saz文件结尾数据(能被fiddler软件识别) ...

  6. [Swift通天遁地]一、超级工具-(1)动态标签:给UILabel文字中的Flag和url添加点击事件

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  7. Go语言多态

    总结一下Go语言中多态 package main import "fmt" //申明一个函数类型 type FuncMs func(int ,int) int //加法 func ...

  8. (图论)51NOD 1298 圆与三角形

    给出圆的圆心和半径,以及三角形的三个顶点,问圆同三角形是否相交.相交输出"Yes",否则输出"No".(三角形的面积大于0).     输入 第1行:一个数T, ...

  9. (图论)51NOD 1212 无向图最小生成树

    N个点M条边的无向连通图,每条边有一个权值,求该图的最小生成树. 输入 第1行:2个数N,M中间用空格分隔,N为点的数量,M为边的数量.(2 <= N <= 1000, 1 <= M ...

  10. 扩展KMP的应用

    扩展KMP的应用: 给出模板串S和串T,长度分别为Slen和Tlen,要求在线性时间内,对于每个S[i](0<=i<Slen),求出S[i..Slen-1]与T的 最长公共前缀长度,记为e ...