python 数据的清理行为实例详解
作者:cakin24 时间:2021-04-12 08:43:50
python 数据的清理行为实例详解
数据清洗主要是指填充缺失数据,消除噪声数据等操作,主要还是通过分析“脏数据”产生的原因和存在形式,利用现有的数据挖掘手段去清洗“脏数据”,然后转化为满足数据质量要求或者是应用要求的数据。
1、try 语句还有另外一个可选的子句,它定义了无论在任何情况下都会执行的清理行为。
例如:
>>>try:
raiseKeyboardInterrupt
finally:
print('Goodbye, world!')
Goodbye, world!
Traceback(most recent call last):
File"<pyshell#71>", line 2,in<module>
raiseKeyboardInterrupt
KeyboardInterrupt
以上例子不管try子句里面有没有发生异常,finally子句都会执行。
2、如果一个异常在 try 子句里(或者在 except 和 else 子句里)被抛出,而又没有任何的 except 把它截住,那么这个异常会在 finally 子句执行后再次被抛出。
下面是一个更加复杂的例子(在同一个 try 语句里包含 except 和 finally 子句):
>>>def divide(x, y):
try:
result = x / y
exceptZeroDivisionError:
print("division by zero!")
else:
print("result is", result)
finally:
print("executing finally clause")
>>> divide(2,1)
result is2.0
executing finally clause
>>> divide(2,0)
division by zero!
executing finally clause
>>> divide("2","1")
executing finally clause
Traceback(most recent call last):
File"<pyshell#91>", line 1,in<module>
divide("2","1")
File"<pyshell#88>", line 3,in divide
3、预定义的清理行为
一些对象定义了标准的清理行为,无论系统是否成功的使用了它,一旦不需要它了,那么这个标准的清理行为就会执行。
这面这个例子展示了尝试打开一个文件,然后把内容打印到屏幕上:
>>>for line in open("myfile.txt"):
print(line, end="")
Traceback(most recent call last):
File"<pyshell#94>", line 1,in<module>
for line in open("myfile.txt"):
FileNotFoundError:[Errno2]No such file or directory:'myfile.txt'
以上这段代码的问题是,当执行完毕后,文件会保持打开状态,并没有被关闭。
关键词 with 语句就可以保证诸如文件之类的对象在使用完之后一定会正确的执行他的清理方法:
>>>with open("myfile.txt")as f:
for line in f:
print(line, end="")
Traceback(most recent call last):
File"<pyshell#98>", line 1,in<module>
with open("myfile.txt")as f:
FileNotFoundError:[Errno2]No such file or directory:'myfile.txt'
以上这段代码执行完毕后,就算在处理过程中出问题了,文件 f 总是会关闭。
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
标签:python,数据清理
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python选择网卡发包及接收数据包
2021-10-13 12:02:10
JavaScript画圆
2010-01-22 15:57:00
将MySQL的作为文件系统使用
2011-12-14 18:34:26
php mysql获取表字段名称和字段信息的三种方法
2023-11-18 22:47:26
![](https://img.aspxhome.com/file/2023/9/104179_0s.png)
python 环境搭建 及python-3.4.4的下载和安装过程
2022-04-24 11:40:44
![](https://img.aspxhome.com/file/2023/8/120928_0s.jpg)
解密ThinkPHP3.1.2版本之模板继承
2023-09-06 16:02:15
python 3利用BeautifulSoup抓取div标签的方法示例
2023-09-17 02:57:48
python使用threading获取线程函数返回值的实现方法
2023-10-28 09:34:21
php 图片上添加透明度渐变的效果
2023-10-23 09:00:02
Python实现分割文件及合并文件的方法
2022-01-28 00:02:02
python实现书法碑帖图片分割
2023-09-12 16:08:02
![](https://img.aspxhome.com/file/2023/0/72850_0s.jpg)
Python实现从URL地址提取文件名的方法
2023-11-28 02:39:48
在python中使用requests 模拟浏览器发送请求数据的方法
2022-05-05 03:17:35
Python中文件遍历的两种方法
2022-01-28 20:34:03
python中通过selenium简单操作及元素定位知识点总结
2021-07-18 15:34:37
![](https://img.aspxhome.com/file/2023/5/95285_0s.png)
OpenCV凸包检测和凸缺陷学习示例
2021-08-20 15:26:20
![](https://img.aspxhome.com/file/2023/6/77656_0s.png)
python爬取cnvd漏洞库信息的实例
2021-08-24 07:18:06
![](https://img.aspxhome.com/file/2023/3/79633_0s.jpg)
django实现web接口 python3模拟Post请求方式
2023-07-28 15:18:14
![](https://img.aspxhome.com/file/2023/9/98829_0s.jpg)
python深度学习tensorflow训练好的模型进行图像分类
2023-02-20 20:40:37
![](https://img.aspxhome.com/file/2023/7/76087_0s.jpg)
教你快速掌握一些方便易用的SQL语句
2008-11-28 15:21:00