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

错误分析:

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. 以太坊客户端geth的基本操作命令

    以太坊客户端geth的基本操作命令搭建了私有链环境之后,整理了一下客户端的一些基本的操作命令: 启动命令重复上篇博客步骤,先将区块链客户端启动,命令如下: geth –datadir “%cd%\ch ...

  2. 并不对劲的uoj276. [清华集训2016]汽水

    想要很对劲的讲解,请点击这里 题目大意 有一棵\(n\)(\(n\leq 50000\))个节点的树,有边权 求一条路径使该路径的边权平均值最接近给出的一个数\(k\) 输出边权平均值下取整的整数部分 ...

  3. BZOJ 1305:dance跳舞(二分+最大流)

    一次舞会有n个男孩和n个女孩.每首曲子开始时,所有男孩和女孩恰好配成n对跳交谊舞.每个男孩都不会和同一个女孩跳两首(或更多)舞曲.有一些男孩女孩相互喜欢,而其他相互不喜欢(不会“单向喜欢”).每个男孩 ...

  4. Python进程、线程、协程的对比

    1. 执行过程 每个线程有一个程序运行的入口.顺序执行序列和程序的出口.但是线程不能够独立执行,必须依存在进程中,由进程提供多个线程执行控制.每个线程都有他自己的一组CPU寄存器,称为线程的上下文,该 ...

  5. 推荐使用集串口,SSH远程登录和FTP传输三合一工具MobaXterm

    在以前的资料里,串口和SSH远程登使用SecureCRT,window与ubuntu数据传输使用filezilla,窗口切换来切换去,麻烦也眼花缭乱.有没有一个工具搞定串口.SSH和FTP?有!它就是 ...

  6. MFC绘图

    //20171/121 两点一线 比如鼠标左击和鼠标弹起的两个消息 然后响应从而获取一条线2 添加响应函数方法 类图->右击->addwindowsmessage3 Dview和main中 ...

  7. linux下系统调用劫持ioctl

    实验环境:linux 2.6.32   64位系统 采用lkm(动态加载内核模块)方式劫持ioctl系统调用,系统调用过程如图所示(以open为例子) 实验代码:(头文件有不需要的,但是懒得改了,在系 ...

  8. E20170524-hm

    logging   n. <美>伐木搬运业; 记录,存入; 航行日志; inversion  n. 倒置; 转化; (尤指词序) 倒装; (染色体的) 倒位; reversion   n. ...

  9. bzoj 2216: [Poi2011]Lightning Conductor【决策单调性dp+分治】

    参考:https://blog.csdn.net/clove_unique/article/details/57405845 死活不过样例看了题解才发现要用double.... \[ a_j \leq ...

  10. 线程池之ThreadPoolExecutor使用

    ThreadPoolExecutor机制  一.概述 1.ThreadPoolExecutor作为java.util.concurrent包对外提供基础实现,以内部线程池的形式对外提供管理任务执行,线 ...