python with as 进行文件读取
来源:互联网 发布:淘宝注册流程图 编辑:程序博客网 时间:2024/06/08 16:58
1. 用with...as...来做文件内容的读取(在内容不多的情况下,如果文件巨大,还是一行一行读取吧)
filename = os.path.join(self._data_path, 'Annotations', index + '.txt'with open(filename) as f: lines = [x.strip() for x in f.readlines() if string.atoi(x.strip().split()[2]) > 3]
这样两行语句就可以完成读写工作,简介明了。 并且with...as...语句内置了__enter__ 和__exit__操作,会自动关闭file,所以不用显示调用:
f.close()
0 0
- python with as 进行文件读取
- Python:打开文件读取每一行(使用with as)
- 读取文件、OS模块、异常处理、with as语法
- python with...as...用法
- python with as语法
- python with/as
- python with as 使用
- python with as
- python with...as用法
- python with as语法
- python with...as...用法
- python with...as...用法
- python with as 用法
- python with as用法
- python with..as 语法
- python with as语法
- python with as 用法
- python基础--with-as
- eclipse 统一修改 java 文件编码格式
- python str bytes unicode utf-8 学习
- 笔记(2)
- scroll事件在微信安卓端的研究
- Ubuntu下网络设定
- python with as 进行文件读取
- SLF4J: Class path contains multiple SLF4J bindings.
- toolbar详解
- OWASP ZAP上手体验
- 浮点型在内存当中的存储方式
- aiohttp
- zynq-7000学习笔记(五)——制作BOOT.bin文件
- hdu 1757
- 用java实现地理位置转换,坐标转换,行驶距离计算(高德地图)