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
随机推荐
- 手撕Vue-Router-初始化路由信息
前言 经过上一节课的学习,我们已经完成了提取我们想要的路由信息数据格式,提取完毕了之后,接下来我们该干什么,接下来需要做的步骤就是监听路由的变化,保存当前的路由. 那么就会遇到几个问题,就是怎么监听, ...
- python之特殊属性和特殊方法
目录 特殊属性 __dict__查看属性和方法 __class__查看对象所属类 __bases__查看子类的父类 __mro__查看类的层次结构 __subclasses__查看父类被继承的子类 特 ...
- C语言编写一个程序,从键盘读入一个矩形的两个边的值(整数),输出矩形面积。
#include<stdio.h> int main() { int i, j, s;//定义矩形的长,宽,面积变量 scanf_s("%d,%d", &i, ...
- 使用nacos配置无效,原因:项目中 gateway服务配置的 application的name:@artifactId@ 和nacos上配置的DataID 不一致导致
遇到一个问题,项目启动后一致无法正常登陆进入后端,登陆时一直报错返回null,排查后发现是自己粗心,项目中 gateway服务配置的 application的name:@artifactId@ 和 ...
- SpringBoot使用maven打jar包配置
在pom.xml文件中加入依赖 <parent> <groupId>org.springframework.boot</groupId> <artifactI ...
- 解密数据可视化软件、BI软件和数字孪生软件的不同
在现代企业和科技领域,数据起着至关重要的作用.为了更好地管理和理解数据,不同类型的软件工具应运而生,其中包括数据可视化软件.BI(Business Intelligence)软件和数字孪生软件.虽然它 ...
- 面向对象(OOP)
面向对象 面向对象 面向过程 & 面向对象 面向过程思想 步骤清晰简单,第一步做什么,第二步做什么... 面对过程适合处理一些较为简单问题 面向对象思想 物以类聚,分类的思维模式,思考问题首先 ...
- ElasticSearch之配置
ElasticSearch主要的配置文件,如下: elasticsearch.yml,ElasticSearch的相关参数. jvm.options,JVM的相关参数. log4j2.properti ...
- 【scikit-learn基础】--『监督学习』之 支持向量机回归
在机器学习中,支持向量机(Support Vector Machine)算法既可以用于回归问题,也可以用于分类问题. 支持向量机(SVM)算法的历史可以追溯到1963年,当时前苏联统计学家弗拉基米尔· ...
- 部署堡垒机3——编译安装redis-6.2.1以上版本
一.环境准备 Redis官网:https://redis.io/download/ 历史版本:http://download.redis.io/releases/ 1.安装依赖 yum -y in ...