#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标准库基础学习的更多相关文章

  1. python calendar标准库基础学习

    # -*- coding: utf-8 -*-# 作者:新手__author__ = 'Administrator'#标准库:日期时间基础学习:calendar:处理日期#例1import calen ...

  2. python StringIO标准库基础学习

    #标准库:StringIO提供类文件API文本缓冲区#作用:可以处理内存中的文本,有2种不同的实现:cStringIP版本用c编写提高速度,StringIO用python来提供可移植性,与其他字符串连 ...

  3. python glob标准库基础学习

    #glob文件名模式匹配#作用:使用unix shell规则查找与一个模式匹配文件名"""尽管glob api很小,但这个模块很强大,只要程序需要查找文件系统中名字与某种 ...

  4. python filecmp标准库基础学习

    # -*- coding: utf-8 -*-# 作者:新手__author__ = 'Administrator'#文件的比较import os,filecmp#作用用于比较系统中的目录和文件#例子 ...

  5. python 标准库基础学习之开发工具部分1学习

    #2个标准库模块放一起学习,这样减少占用地方和空间#标准库之compileall字节编译源文件import compileall,re,sys#作用是查找到python文件,并把它们编译成字节码表示, ...

  6. 【python】标准库的大致认识

    正如那句 Python 社区中很有名的话所说的:“battery included”,Python 的一大好处在于它有一套很有用的标准库(standard library).标准库是随着 Python ...

  7. C 标准库基础 IO 操作总结

    其实输入与输出对于不管什么系统的设计都是异常重要的,比如设计 C 接口函数,首先要设计好输入参数.输出参数和返回值,接下来才能开始设计具体的实现过程.C 语言标准库提供的接口功能很有限,不像 Pyth ...

  8. Python的标准库介绍与常用的第三方库

    Python的标准库介绍与常用的第三方库 Python的标准库: datetime:为日期和时间的处理提供了简单和复杂的方法. zlib:以下模块直接支持通用的数据打包和压缩格式:zlib,gzip, ...

  9. python 使用标准库连接linux实现scp和执行命令

    import stat import pexpect 只显示关键代码: sqldb = localpath+database //获取database名字 if os.path.exists(sqld ...

随机推荐

  1. java\C#\php主流语言实现FMS流媒体传输协议RTMP的开源组件

    java:bladeDS http://sourceforge.net/adobe/blazeds/wiki/Home/ .net:FlourinceFX http://www.fluorinefx. ...

  2. Netmon: A light-weight network monitor for Windows

    Netmon is a light-weight network monitor that works on Windows operating systems. It provides differ ...

  3. HashMap的分析(转)

    一.HashMap概述 HashMap基于哈希表的 Map 接口的实现.此实现提供所有可选的映射操作,并允许使用 null 值和 null 键.(除了不同步和允许使用 null 之外,HashMap  ...

  4. Struts的核心配置

    一.配置struts.xml文件 1.struts.xml文件 2.常量配置 <constant> struts.properities web.xml中的<init-param&g ...

  5. MySQL 可以用localhost 连接,但不能用IP连接的问题,局域网192.168.*.* 无法连接mysql

    Mysql 默认是没有开启这个权限的(只允许使用 host:localhost,或者 host:127.0.0.1),如果想用 host:192.168.1.* ,来访问mysql ,需要手动开启这个 ...

  6. jquery之营销系统(补偿记录)

    var appPath = getAppPath(); $(function(){ $("#opreateHtml").window("close"); $(& ...

  7. DataSet ,DataTable,DataRow 之间的关系与使用

    关系   DataSet 包含多个DataTable,DataTable包含多行DataRow. 使用情况:   有时候GridView等控件需要将数据源动态绑定到DataSet中:将多个DataSe ...

  8. SqlServer跨域查询

    SELECT * FROM OPENDATASOURCE('SQLOLEDB','Data Source=192.168.1.14;User ID=sa;Password=sql.com').eBui ...

  9. android 自定义view之 TypeArray

    在定义view的时候,我们可以使用系统提供的属性,也可以自定义些额外的属性来设置自定义view的样式,这个时候,我们就需要TypeArray,字面意思就是Type 数组. 今天我们就讲讲如何自定义Vi ...

  10. C#Socket 案例

    服务器端 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; ...