python linecache标准库基础学习
#python标准库基础之:linecacge:高效读取文本文件
#说明与作用
"""
可以从文件或者导入python模块获取文件,维护一个结果缓存,从而可以更高效地从相同文件读取多行文本;
此模块会在python标准库的其他部分中用到,缓存实现将在内存中保存文件内容(解析为单独的行
).API通过索引一个列表返回所请求的行。与反复地读取文件并解析文本来查找所需文本行相比,
这样可以节省时间,这个方法在查找同一个文件中多行尤其有用 ,比如一个异常.
"""
import linecache,tempfile,os
#文本,上网或者自己写
# text='My father was a self-taught mandolin player. ' \
# 'He was one of the best string instrument players in our town. ' \
# 'He could not read music, but if he heard a tune a few times, ' \
# 'he could play it. When he was younger, he was a member of a small country ' \
# 'music band. They would play at local dances and on a few occasions ' \
# 'would play for the local radio station. He often told us how he had ' \
# 'auditioned and earned a position in a band that featured Patsy Cline as ' \
# 'their lead singer. He told the family that after he was hired he never ' \
# 'went back. Dad was a very religious man. He stated that there was a lot ' \
# 'of drinking and cursing the day of his audition and he did not want to ' \
# 'be around that type of environment.'
# def make_file():
# fd,temp=tempfile.mkstemp()
# os.close(fd)
# with open(temp,'wt') as f:
# try:
# f.write('%s'%(text))
# finally:
# f.close()
# return temp
#
# def cleanup(filename):
# os.unlink(filename)
#读取特定的行
#linecache模块读取文件 行号从1开始,不过通常列表数组索引会从0开始
from datatext import *
filename=make_file()
print 'source:'
print '%r'%text.split('\n')[0]
print
print 'cache:'
print '%r'%linecache.getline(filename,5)
cleanup(filename)
#处理空行
"""
返回在行末都包括一个换行符,所以如果文本行为空,那么返回值就是一个换行符
"""
print 11
print linecache.getline(filename,2)
#错误处理
#如果所请求的行号走出文件中合法行号范围,getline()会返回空串
not1=linecache.getline(filename,100)
print '(%s)'%(not1,len(not1))
#文件行只有12行,所有请教到第n+1行就像试图超过文件末尾继续读取文件
#读取一个没有存在的文件,也可以使用这样的方法
such_file=linecache.getline('a.txt')
print u'无此文件:%s'%(such_file)
#读取python源文件
"""
由于lincache在生成traceback跟踪记录时使用相当频繁,其关键特性之王就是能够通过指定模块其名在导入路径中查找python源模块!
"""
file_src=linecache.__file__
if file_src.endswith('.pyc'):
file_src=file_src[:-1]
print '\nFILE:'
with open(file_src,'r')as f:
file_line=f.readlines()[2]
print repr(file_line)
"""
如果此模块linecache中缓存填充代码在当前目录中无法指定指定名文件,它会在sys.path中搜索指定的模块,这个例子要查找a.py,由于当前目录中
没有这个文件副本,所以会找到标准中相应文件.
"""
#linecache官方标准库地址:https://docs.python.org/2.7/library/linecache.html?highlight=linecache#module-linecache
#lorem ipsum生成器地址如下:http://www.ipsum.com/ #注意:此地址(可能需要FQ或者可能不存在)
python linecache标准库基础学习的更多相关文章
- python calendar标准库基础学习
# -*- coding: utf-8 -*-# 作者:新手__author__ = 'Administrator'#标准库:日期时间基础学习:calendar:处理日期#例1import calen ...
- python StringIO标准库基础学习
#标准库:StringIO提供类文件API文本缓冲区#作用:可以处理内存中的文本,有2种不同的实现:cStringIP版本用c编写提高速度,StringIO用python来提供可移植性,与其他字符串连 ...
- python glob标准库基础学习
#glob文件名模式匹配#作用:使用unix shell规则查找与一个模式匹配文件名"""尽管glob api很小,但这个模块很强大,只要程序需要查找文件系统中名字与某种 ...
- python filecmp标准库基础学习
# -*- coding: utf-8 -*-# 作者:新手__author__ = 'Administrator'#文件的比较import os,filecmp#作用用于比较系统中的目录和文件#例子 ...
- python 标准库基础学习之开发工具部分1学习
#2个标准库模块放一起学习,这样减少占用地方和空间#标准库之compileall字节编译源文件import compileall,re,sys#作用是查找到python文件,并把它们编译成字节码表示, ...
- 【python】标准库的大致认识
正如那句 Python 社区中很有名的话所说的:“battery included”,Python 的一大好处在于它有一套很有用的标准库(standard library).标准库是随着 Python ...
- C 标准库基础 IO 操作总结
其实输入与输出对于不管什么系统的设计都是异常重要的,比如设计 C 接口函数,首先要设计好输入参数.输出参数和返回值,接下来才能开始设计具体的实现过程.C 语言标准库提供的接口功能很有限,不像 Pyth ...
- Python的标准库介绍与常用的第三方库
Python的标准库介绍与常用的第三方库 Python的标准库: datetime:为日期和时间的处理提供了简单和复杂的方法. zlib:以下模块直接支持通用的数据打包和压缩格式:zlib,gzip, ...
- python 使用标准库连接linux实现scp和执行命令
import stat import pexpect 只显示关键代码: sqldb = localpath+database //获取database名字 if os.path.exists(sqld ...
随机推荐
- JAVA的RSA加密算法工具类
须要用到一个jar http://www.bouncycastle.org/latest_releases.html 须要注意的问题 JS用同一秘钥生成的密文用java解密出来是逆序的,即js加密12 ...
- 关于退运美国转基因玉米含有MRI 162转基因成分的质疑
6月30日,新华社刊出文章"我国退运125.2万吨进口美国转基因玉米",读后有感. 文章说:国家质检总局办公厅副主任陆春明30日介绍,截至今年6月16日,全国出入境检验检疫机构共在 ...
- Qt的Graphics-View框架和OpenGL结合详解
Qt的Graphics-View框架和OpenGL结合详解 演示程序下载地址:这里 程序源代码下载地址:这里 这是一篇纯技术文,介绍了这一个月来我抽时间研究的成果. Qt中有一个非常炫的例子:Boxe ...
- 邮件协议(SMTP)性能测试总结(Foxmail邮箱)
先介绍一下邮件协议SMTP的工作机制(连接和发送过程),用wireshark工具抓包进行分析,如下: SMTP协议的工作机制(连接和发送过程): 1.建立TCP连接,并将邮件服务器地址给客户端: 2. ...
- prototype vs __proto__ 之间关系
__proto__ is the actual object that is used in the lookup chain to resolve methods, etc. __proto__是解 ...
- SSH Secure Shell Client连接Linux 命令行显示中文乱码问题 和oracle 查询数据中文乱码问题
一.SSH Secure Shell Client连接Linux 命令行显示中文乱码问题 linux 设置系统语言 修改 /etc/sysconfig/i18n 文件,如 LANG="en_ ...
- javascript 获取图片原始尺寸
javascript 获取图片原始尺寸 function getImgInfo(url){ var img = new Image(), loaded = false; var info = {}; ...
- log4j.property配置
# 1. 日志等级 FATAL=0; ERROR=3; WARN=4; INFO=6; DEBUG=7; # 2. Appender 为日志输出目的地,Log4j提供的appender有以下几种# o ...
- uva 10370 - Above Average
#include <iostream> #include <cstdio> using namespace std; int main() { unsigned C, N, t ...
- web标准(复习)--2 列布局
今天我们开始学习一列布局,包含以下几种形式: 1.一列固定宽度 2.一列固定宽度居中 3.一列自适应宽度 4.一列自适应宽度居中 5.一列二至多块布局 前一节我们回顾了xhtml基础和css基础部分, ...