python中遇到的bug

TypeError

解决问题:
TypeError:draw_networkx_nodes() got an unexpected keyword argument ‘with_labels’
解决思路:
类型错误:draw_network_nodes()得到意外的关键字参数’with_labels’
解决方法:
删掉‘with_labels’即可

AttributeError

解决问题:
AttributeError: module ‘community’ has no attribute ‘best_partition’
解决方法:
pip install python-louvain

解决问题:
AttributeError: ‘NoneType’ object has no attribute ‘shape’
问题描述:

import cv2
img = cv2.imread('a')
print(img.shape)

解决方法:
找错时发现,img = cv2.imread(‘a.jpg’)的返回值img为none,故根本就没有成功读取图片,网上的解决方法都是说绝对路径都中文或者是用了单斜杠(\是转义字符),但自己两种问题都不存在,最后发现路径名中的文件名没有后缀,或者说文件名加后缀不对应。

import cv2
img = cv2.imread('a.jpg')
print(img.shape)

TypeError

在跑K-means算法时遇到
在这里插入图片描述
解决问题:
TypeError: unsupported operand type(s) for -: ‘map’ and ‘map’
解决思路:

fltLine = map(float, curLine)

在python2中返回的是一个list类型数据,而在python3中该语句返回的是一个map类型的数据(即是地址)故需要将数据类型转换为列表型。
解决方法:

fitLine = list(map(float,curLine))

链接

ValueError

紧跟着又出现值错误
解决问题:
ValueError: could not convert string to float: ‘1.658985 4.285136’
解决方法:
curLine = line.strip().split(’\t ‘)改成curLine = line.strip().split(’ ')
链接

AttributeError

解决问题:
AttributeError: module ‘string’ has no attribute ‘atoi’
解决思路:
atoi函数是将str转化为int,python3中不支持,而用int()转换在python2,python3中都支持。
解决方法:
string.atoi(your_str)替换成int(your_str)
链接

TypeError

解决问题:
TypeError:dict_keys’ object is not subscriptable
具体问题:

dict1 = {1:1}
dict2 = {1:2}
if (dict1.values()[0] < dict2.values()[0]):
 print(dict1)

解决思路:
object is not subscriptable意思是对象不应该具有下标,原因是dict的value()和key()返回的并不是一个真正的数组,所以,处理方法也很简单,只需要用list()强制转换一下即可。
解决方法:

dict1 = {1:1} 
dict2 = {1:2} 
if (list(dict1.values())[0] < list(dict2.values())[0]):
 print(dict1)

链接

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值