Python报错:AttributeError: type object 'str' has no attribute '_name_'(机器学习实战treePlotter代码)解决方案
错误信息:
学习《机器学习实战》这本书时,按照书上的代码运行,产生了错误,但是在代码中没有错误提示,产生错误的代码如下:
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代码)解决方案的更多相关文章
- 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 ...
- 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_ ...
- Python - celery 相关报错 - AttributeError: type object '_multiprocessing.win32' has no attribute 'WAIT_OBJECT_0'
报错场景 执行 celery worker -A tasks -l INFO 打开 worker 的时候报错无法进行 报错解决 Celery 的版本过高, 进行降级处理即可 pip instal ...
- python报错“AttributeError: 'set' object has no attribute 'items'“
作为才开始学爬虫的萌新,遇到了一个这样的错,很懵逼 后面到网络到处查看大佬的解决方法,才发现headers的请求头部信息有错误,headers是一个字典,不是字符串,所以报错了 原代码 headers ...
- dnspython模块报错 AttributeError: 'CNAME' object has no attribute 'address'
有时候用到这个模块的时候会报错 AttributeError: 'CNAME' object has no attribute 'address' 如下所示 [root@ansible ch01]# ...
- py+selenium 明明定位不到元素,但却不报错或是报错AttributeError: 'list' object has no attribute 'click'【已解决】
问题:定位不到元素,但却不报错或者出现报错AttributeError: 'list' object has no attribute 'click' 如图 或者 解决方法: 将”driver ...
- AttributeError: type object 'testClass' has no attribute 'testMothod'
点击"Unittest for test_post_API.testClass"按钮,点击”Edit configuration...“,弹出对话框Run/Debug config ...
- AttributeError type object 'deprecated' has no attribute 'ROCKY'
AttributeError type object 'deprecated' has no attribute 'ROCKY' 在使用kolla安装docker的时候遇到了AttributeErro ...
- python文件名不要跟模块名相同,报错AttributeError: 'module' object has no attribute 'Differ'
python中的文件都会生成pyc文件,包括模块也是这样,所以调用模块的时候,实际上会调用模块.pyc文件:在这个前提下,如果将文件名命名成跟模块名一样,在同一目录下就会生成一个跟模块名一样的pyc文 ...
- Rasa init报错:AttributeError: type object 'Callable' has no attribute '_abc_registry'
错误:Rasa init --no-prompt 报错 原因:Python升级到3.7后会遇到该问题 解决:pip uninstall typing
随机推荐
- Spring Boot 关闭 Actuator ,满足安全工具扫描
应用被安全工具,扫描出漏洞信息 [MSS]SpringBoot Actuator敏感接口未授权访问漏洞(Actuator)事件发现通告: 发现时间:2023-11-25 19:47:17 攻击时间:2 ...
- 有哪些可部署的, 无需编程的,基于WEB的数据可视化工具
基于Web的数据可视化工具在当今数字化时代具有重要的作用,可以帮助企业和个人更好地理解和利用数据.以下是一些无需编程即可部署的基于Web的数据可视化工具,详细描述如下:Tableau Public: ...
- IIS安装与配置
一.环境介绍 Windows Server 2019 64位 标准版 二.IIS安装 2.1.打开服务器管理器,单击添加角色和功能 在Windows Server 2019 服务器管理中,点击角色和功 ...
- The request is not allowed by the user agent or the platform in the current context, possibly because the user denied permission,iphone手机video标签报错
The request is not allowed by the user agent or the platform in the current context, possibly becaus ...
- SpringBoot获取启动类Application所在包路径
1. @SpringBootApplication 注解中引用了@EnableAutoConfiguration 注解. 2.查看 @EnableAutoConfiguration 注解,发现引用了 ...
- OpenTiny Vue 3.12.0 发布:文档大优化!增加水印和二维码两个新组件🎈
你好,我是 Kagol. 非常高兴跟大家宣布,2023年11月30日,OpenTiny Vue 发布了 v3.12.0 . OpenTiny 每次大版本发布,都会给大家带来一些实用的新特性,10.24 ...
- Python+Selenium+Webdriver+unittest 实现登录126邮箱
第一版:登录 #encoding=utf-8 import unittest import time from selenium import webdriver from selenium.webd ...
- 【UniApp】-uni-app-路由
前言 好,经过上个章节的介绍完毕之后,了解了一下 uni-app-CompositionAPI应用生命周期和页面生命周期 那么了解完了uni-app-CompositionAPI应用生命周期和页面生命 ...
- Mybatis-Flex核心功能之@Id
1.是什么? 在 Entity 类中,MyBatis-Flex 是使用 @Id 注解来标识主键的 2.怎么玩? public @interface Id { /** * ID 生成策略,默认为 non ...
- Android联系人增删改查
1:申请权限 <uses-permission android:name="android.permission.READ_CONTACTS"/> <uses-p ...