使用python中常见的错误及处理办法(1)

来源:互联网 发布:哪些网络销售软件好用 编辑:程序博客网 时间:2024/05/29 16:08

(1)invalid literal for int( ) with base 10:'largeDoses'

解决办法:这是因为数据类型不一致,导致出错,在int/str/float转换,其中raw_input接收字符串,input接收整数

(2)‘                      ^

       syntaxError:invalid syntax‘

解决办法:可能是标点符号、括号、双引号等不配对,需要增加或者删除

(3)‘ NameError:name'sorted Dist Indicies' is not defined ’

解决办法:这是因为变量前后不匹配,需要增加或者减少某个字母;有可能是前后大小写不同而引起不能识别

(4)‘dict_keys’ object does't support indexing

解决办法:这是因为python版本不一致,所以对列表的索引出现问题

例:firstsides = mytree.keys( )[0] 应该改为:firstsides = list(mytree.keys( ))[0]

(5)‘write( ) argument must be str, not byes’

解决办法:pickle( ) 是二进制格式,故带'b'标志打开文件‘wb/rb’

(6)同一目录下相互调用函数的做法是

-------先import 文件名

-------再文件名.函数

例:import treeplotter

       treeplotter.createplot(lensesTree)

(7)‘date type not understood!’

例:returnMat  = zeros(numberoflines, 3)

修改为:returnMat  = zeros((numberoflines, 3)),别忘了外面的括号!!!

(8)‘ return arrays must be of ArrayType’

例:shannoEnt -= prob * log(prob,2)

修改为:shannoEnt -= prob * np.math.log(prob,2)

(9)’can't multiply sequence by non-int of type float‘’

解决办法:需要将出错的地方转换数据类型

(10)'itertools cycle' object has no attribute 'next'

原因:编辑版本的原因,出现差错

例:idexer.next( )

修改为:next(idexer)

(11)‘list index out of range’

原因:(1)list[index],index超出范围

           (2)list是空列表,进行list[0]就会出错

(12)‘‘int’ object is not iterable’

例:for i in hosts:

修改为:for i in range(hosts):

(13)'unindent does not mach any outer indentation level '

原因:注意缩进,在某行的开头处可能被空格了一下

(14)'could not convert string to float '

原因:(1)可能是文本格式的问题----数据应先在外边的文本.txt编辑后再导入,否则就会引起这种错误的提示

   (2)可能在数据分割时出错,将split('\n')改成split(',')

(15)'only length-1 arrays can be converted to python scalars'

原因:不要将列表转换成矩阵形式,

例:math.log(a) ----->np.log(a)

(16)list( ) takes at most 1 argument (2 given)

原因:list 至多1个参数








原创粉丝点击