错误信息:

学习《机器学习实战》这本书时,按照书上的代码运行,产生了错误,但是在代码中没有错误提示,产生错误的代码如下:

if type(secondDict[key])._name_ == 'dict':

报错如下:

AttributeError: type object 'str' has no attribute '_name_'

把错误信息翻译一下:属性错误:类型对象“ str ”没有属性“name”,

错误产生是因为版本不同,作者使用的是2.x版本,而我使用的是3.7.x版本。

解决方案

Python3中类型对象“ str ”没有“_name_”属性,所以我们需要将属性去掉。除此之外,这句判断的语句本来是判断该节点类型是否为dict(字典),但是因为加上了单引号(‘’),就变成字符串了,这样会产生其他错误,所以我们也需要将单引号去掉。

把错误代码修改成如下:

if type(secondDict[key]) == dict:

此时运行就可以了。

Python报错:AttributeError: type object 'str' has no attribute '_name_'(机器学习实战treePlotter代码)解决方案的更多相关文章

  1. ddt运行报错AttributeError: type object 'TestLogin' has no attribute 'test_login'

    源代码: #!usr/bin/python3 # -*- coding:utf-8 -*- # @Time: 2018/12/17 下午2:07 # @File: do_excel.py # @Sof ...

  2. ddt运行测试方法时报错AttributeError: type object 'TestHttpRq' has no attribute 'test_http_rq_login'

    import unittest import ddt #装饰器 from ddt import ddt,data,unpack #导入ddt中的各个模块 from homework.unittest_ ...

  3. Python - celery 相关报错 - AttributeError: type object '_multiprocessing.win32' has no attribute 'WAIT_OBJECT_0'

    报错场景 执行   celery worker -A tasks -l INFO  打开 worker 的时候报错无法进行 报错解决 Celery 的版本过高, 进行降级处理即可 pip instal ...

  4. python报错“AttributeError: 'set' object has no attribute 'items'“

    作为才开始学爬虫的萌新,遇到了一个这样的错,很懵逼 后面到网络到处查看大佬的解决方法,才发现headers的请求头部信息有错误,headers是一个字典,不是字符串,所以报错了 原代码 headers ...

  5. dnspython模块报错 AttributeError: 'CNAME' object has no attribute 'address'

    有时候用到这个模块的时候会报错 AttributeError: 'CNAME' object has no attribute 'address' 如下所示 [root@ansible ch01]# ...

  6. py+selenium 明明定位不到元素,但却不报错或是报错AttributeError: 'list' object has no attribute 'click'【已解决】

    问题:定位不到元素,但却不报错或者出现报错AttributeError: 'list' object has no attribute 'click' 如图  或者  解决方法:   将”driver ...

  7. AttributeError: type object 'testClass' has no attribute 'testMothod'

    点击"Unittest for test_post_API.testClass"按钮,点击”Edit configuration...“,弹出对话框Run/Debug config ...

  8. AttributeError type object 'deprecated' has no attribute 'ROCKY'

    AttributeError type object 'deprecated' has no attribute 'ROCKY' 在使用kolla安装docker的时候遇到了AttributeErro ...

  9. python文件名不要跟模块名相同,报错AttributeError: 'module' object has no attribute 'Differ'

    python中的文件都会生成pyc文件,包括模块也是这样,所以调用模块的时候,实际上会调用模块.pyc文件:在这个前提下,如果将文件名命名成跟模块名一样,在同一目录下就会生成一个跟模块名一样的pyc文 ...

  10. Rasa init报错:AttributeError: type object 'Callable' has no attribute '_abc_registry'

    错误:Rasa init --no-prompt 报错 原因:Python升级到3.7后会遇到该问题 解决:pip uninstall typing

随机推荐

  1. Linux-目录层次标准

    版权声明:原创作品,谢绝转载!否则将追究法律责任. ----- 作者:kirin 根目录(/) 根目录是整个系统最重要的一个目录,因为不但所有的目录都是由根目录衍生出来的,同时根目录也与开机.还原.系 ...

  2. python简介和基本数据类型

    今天是2018年12月7日,开始python的学习,现在将知识点总结如下: 1  python语言有2个版本分别是 python2 .python3    区别还是很大的,例如 python2 中 1 ...

  3. 吉特日化MES & 医药行业相关专业术语汇总

  4. [CF1830F] The Third Grace

    题目描述 You are given $ n $ intervals and $ m $ points on the number line. The $ i $ -th intervals cove ...

  5. 媒体img组件以及swiper轮播

    .swiper{        height: 400rpx;        margin-top: 100rpx;        .item{            padding: 20rpx;  ...

  6. 开源 Serverless 框架 Laf 性能优化实践

    介绍 Laf 是一个完全开源的 Serverless 框架,Laf 的 Node.js 运行时容器 (以下简称为 Runtime) 是 Laf 的函数执行环境,依托于 Express.js 框架.采用 ...

  7. LeetCode331:验证二叉树的前序序列化(递归)

    解题思路:把所有元素存成数组,设置一个全局下标next,表示当前节点如果要继续遍历应当从数组的哪个位置开始,然后从下标 0 开始DFS.如果DFS返回真并且next下标等于数组的长度,说明元素已经全部 ...

  8. 微软官方发布的C#开源、免费、实用的Windows工具箱

    前言 今天分享一款由微软官方发布的C#开源.免费.实用的Windows工具箱(帮助用户调整和简化Windows系统的体验,从而提高工作效率):Microsoft PowerToys. 项目介绍 Mic ...

  9. 用JS实现简单的屏幕录像机

    本文将介绍如何用JS实现简单的屏幕录像机. 一.录制准备 创建一个按钮 <button id="recording-toggle">Start recording< ...

  10. Nginx服务器常用参数设置

    Nginx作为一个高性能的Web服务器和反向代理,它的性能可以通过调整底层操作系统的参数来进一步优化.以下是一些常见的操作系统级别的调整,通常针对Linux系统: File Descriptors L ...