Python中TypeError: Unsupported data type: class NoneType和IndentationError: unexpected indent两个异常
2019-07-06 14:38
1256 查看
运行出现如下报错 TypeError: Unsupported data type: <class 'NoneType'>
python2的编译报错如下:直接截图:
python3的编译报错如下:
D:\Python\Python36\python.exe D:/PycharmProjects/facetest1/fileface/psutilDemo/totalDemo.py Traceback (most recent call last): File "D:/PycharmProjects/facetest1/fileface/psutilDemo/totalDemo.py", line 10, in <module> ips = IP(ip_s) File "D:\Python\Python36\lib\site-packages\IPy.py", line 262, in __init__ raise TypeError("Unsupported data type: %s" % type(data)) TypeError: Unsupported data type: <class 'NoneType'> Process finished with exit code 1
完整的报错信息:
D:\Python\Python36\python.exe D:/PycharmProjects/facetest1/fileface/psutilDemo/totalDemo.py Traceback (most recent call last): File "D:/PycharmProjects/facetest1/fileface/psutilDemo/totalDemo.py", line 10, in <module> ips = IP(ip_s) File "D:\Python\Python36\lib\site-packages\IPy.py", line 262, in __init__ raise TypeError("Unsupported data type: %s" % type(data)) TypeError: Unsupported data type: <class 'NoneType'> Process finished with exit code 1
我的源代码:
#!/usr/bin/env python from IPy import IP def raw_input(param): pass ip_s = raw_input('Please input an IP or net-range:') ips = IP(ip_s) if len(ips) > 1: print('net: %s' % ips.net()) print('netmask: %s' % ips.netmask()) print('broadcast: %s' % ips.broadcast()) print('reverse address: %s' % ips.reverseNames()[0]) print('subnet: %s' % len(ips)) else: print('reverse address: %s' % ips.reverseName()[0]) print('hexadecimal: %s' % ips.strHex()) print('binary ip: %s' % ips.strBin()) print('iptype: %s' % ips.iptype()) print('ip_version: %s' % ips.version())
第一个报错
刚开始报错。第一次出现得到原因是因为缩进问题,在vim环境下,我设置了一个tab按键占4个字符,不知道为何还是报错:
IndentationError: unexpected indent[root@devops python]# vim pyIPy.py [root@devops python]# python pyIPy.py File "pyIPy.py", line 13 print('subnet: %s' % len(ips)) ^ IndentationError: unexpected indent
后来手动空格才不会出现缩进异常,
IndentationError: unexpected indent
问题就解决了
那么解决最初的异常
TypeError: Unsupported data type: <class 'NoneType'>
在python2的环境下进行代码修改:
#!/usr/bin/env python from IPy import IP # def raw_input(param): # pass # # ip_s = raw_input('Please input an IP or net-range:') # ips = IP(ip_s) ips = IP('192.168.217.0/24') if len(ips) > 1: print('net: %s' % ips.net()) print('netmask: %s' % ips.netmask()) print('broadcast: %s' % ips.broadcast()) print('reverse address: %s' % ips.reverseNames()[0]) print('subnet: %s' % len(ips)) else: print('reverse address: %s' % ips.reverseName()[0]) print('hexadecimal: %s' % ips.strHex()) print('binary ip: %s' % ips.strBin()) print('iptype: %s' % ips.iptype()) print('ip_version: %s' % ips.version()) 输出结果: D:\Python\Python36\python.exe D:/PycharmProjects/facetest1/fileface/psutilDemo/totalDemo.py net: 192.168.217.0 netmask: 255.255.255.0 broadcast: 192.168.217.255 reverse address: 217.168.192.in-addr.arpa. subnet: 256 hexadecimal: 0xc0a8d900 binary ip: 11000000101010001101100100000000 iptype: PRIVATE ip_version: 4 Process finished with exit code 0
结合完整的报错信息又加上刚刚的代码修改可以发现主要问题是出现在第8行代码
先根据报错测试一下:
#!/usr/bin/env python from IPy import IP def raw_input(param): pass ip_s = raw_input('Please input an IP or net-range:') #判断方法 if ip_s is None: print("ip_s is NoneType") 输出结果: ip_s is NoneType 那么就说明我们的raw_input()获取的是一个不存在的参数 Nonetype和空值是不一致的,可以理解为Nonetype为不存在这个参数。 空值表示参数存在,但是值为空。 这里顺便加入一点有关常用的判断测试: type() 方法的语法: type(name, bases, dict),其中name是类的名称,bases是基类的元组,dict是类内定义的命名空间变量。 当有一个参数时它的返回值是对象类型,。有三个参数时,返回值是新的类型对象。 isinstance() 方法的语法: isinstance(object, classinfo),其中object 是实例对象, 变量,classinfo 可以是直接或间接类名、基本类型或者由它们组成的元组(如tuple,dict,int,str,float,list,set,bool,class类等)。 如果对象的类型与classinfo相同则返回 值为True,否则返回值为 False。 isinstance() 与 type() 区别: type() 不会认为子类是一种父类类型,不考虑继承关系。 isinstance() 会认为子类是一种父类类型,考虑继承关系。 如果要判断两个类型是否相同,推荐使用 isinstance()。一般type(x)用来看变量x的类型较多。
修改后的:
#!/usr/bin/env python from IPy import IP # def input(param): # pass # ip_s = raw_input('Please input an IP or net-range:') ip_s = input('Please input an IP or net-range:') #判断 if ip_s is None: print("ip_s is NoneType") ips = IP(ip_s) if len(ips) > 1: print('net: %s' % ips.net()) print('netmask: %s' % ips.netmask()) print('broadcast: %s' % ips.broadcast()) print('reverse address: %s' % ips.reverseNames()[0]) print('subnet: %s' % len(ips)) else: print('reverse address: %s' % ips.reverseName()[0]) print('hexadecimal: %s' % ips.strHex()) print('binary ip: %s' % ips.strBin()) print('iptype: %s' % ips.iptype()) print('ip_version: %s' % ips.version()) 输出结果: D:\Python\Python36\python.exe D:/PycharmProjects/facetest1/fileface/psutilDemo/totalDemo.py Please input an IP or net-range:192.168.217.135 reverse address: 1 hexadecimal: 0xc0a8d987 binary ip: 11000000101010001101100110000111 iptype: PRIVATE ip_version: 4 Process finished with exit code 0
其实就是将raw_input() 更改为input()了
有关raw_input()
input() 的用法区别:
参考:
相关文章推荐
- 【python异常记录】之IndentationError: unexpected indent
- java.lang.UnsupportedClassVersionError: com/kingdee/eas/basedata/org/OrgType (Unsupported major.minor version 49.0)的解决
- 异常备忘:java.lang.UnsupportedClassVersionError: Bad version number in .class file
- java.lang.UnsupportedClassVersionError: Bad version number in .class file异常
- 异常备忘:java.lang.UnsupportedClassVersionError: Bad version number in .class file
- python中super出现的TypeError: must be type, not classobj 原因及解决
- hadoop学习;block数据块;mapreduce实现例子;UnsupportedClassVersionError异常;关联项目源码
- java.lang.UnsupportedClassVersionError: Bad version number in .class file异常
- Python脚本运行出现语法错误:IndentationError:unexpected indent
- Python编译时提示错误 IndentationError: unexpected indent
- 小白python疑难|IndentationError: unexpected indent
- java打jar包的异常一:could not find the main class,java.lang.UnsupportedClassVersionError: Bad version numb
- java.lang.UnsupportedClassVersionError: Bad version number in .class file异常的处理方法
- UnsupportedClassVersionError异常的解决
- opencv-python cv2接口下错误TypeError: mat data type=18 is not supported
- java.lang.UnsupportedClassVersionError: Bad version number in .class file异常
- CXF安装和配置时出现Exception in thread "main" java.lang.UnsupportedClassVersionError:异常?
- python IndentationError: unexpected indent
- 异常备忘:java.lang.UnsupportedClassVersionError: Bad version number in .class file