1. # -*- coding: utf-8 -*-
  2. #python 27
  3. #xiaodeng
  4. #python模块之linecache
  5.  
  6. import linecache
  7. '''
  8. >>> help(linecache)
  9. Help on module linecache:
  10. FUNCTIONS
  11. checkcache = extended_linecache_checkcache(filename=None, orig_checkcache=<function checkcache>)
  12. Extend linecache.checkcache to preserve the <pyshell#...> entries
  13.  
  14. Rather than repeating the linecache code, patch it to save the
  15. <pyshell#...> entries, call the original linecache.checkcache()
  16. (skipping them), and then restore the saved entries.
  17.  
  18. orig_checkcache is bound at definition time to the original
  19. method, allowing it to be patched.
  20.  
  21. clearcache()
  22. Clear the cache entirely.
  23.  
  24. getline(filename, lineno, module_globals=None)
  25.  
  26. DATA
  27. __all__ = ['getline', 'clearcache', 'checkcache']
  28. '''
  29.  
  30. #从名为filename的文件中得到第lineno行。这个函数从不会抛出一个异常–产生错误时它将返回”(换行符将包含在找到的行里)。
  31.  
  32. #linecache.getline(filename,lineno)
  33. #查找特定行(第一行)的数据,返回一个字符串
  34. filename='1.txt'
  35. re= linecache.getline(filename,1)
  36. print re.strip()#因为本身带有换行符
  37.  
  38. #得到所有行数据,返回一个list
  39. re= linecache.getlines(filename)
  40. print re
  41.  
  42. #获取多少行的数据
  43. re= linecache.getlines(filename)[0:4]
  44. print re
  45.  
  46. #更新文件名为filename的缓存。如果filename文件更新了,使用这个函数可以更新linecache.getlines(filename)返回的列表
  47. linecache.updatecache(filename)
  48.  
  49. #清理缓存
  50. linecache.clearcache()

python模块之linecache的更多相关文章

  1. Python模块应用 (linecache)

    linecache linecache是专门支持读取大文件,而且支持行式读取的函数库. linecache 预先把文件读入缓存起来,后面如果你访问该文件的话就不再从硬盘读取.对于大文件的读取效率还不错 ...

  2. Python模块----linecache

    Python标准库提供了一个有趣的模块:linecache模块.该模块用来从文件中读取任何的行,并且将这些lines使用缓存进行优化,常见的情况是从个大文件中读取指定的行.不过由于此模块使用内存进行缓 ...

  3. python模块大全

    python模块大全2018年01月25日 13:38:55 mcj1314bb 阅读数:3049 pymatgen multidict yarl regex gvar tifffile jupyte ...

  4. 使用C/C++写Python模块

    最近看开源项目时学习了一下用C/C++写python模块,顺便把学习进行一下总结,废话少说直接开始: 环境:windows.python2.78.VS2010或MingW 1 创建VC工程 (1) 打 ...

  5. Python模块之configpraser

    Python模块之configpraser   一. configpraser简介 用于处理特定格式的文件,其本质还是利用open来操作文件. 配置文件的格式: 使用"[]"内包含 ...

  6. Python模块之"prettytable"

    Python模块之"prettytable" 摘要: Python通过prettytable模块可以将输出内容如表格方式整齐的输出.(对于用Python操作数据库会经常用到) 1. ...

  7. python 学习第五天,python模块

    一,Python的模块导入 1,在写python的模块导入之前,先来讲一些Python中的概念性的问题 (1)模块:用来从逻辑上组织Python代码(变量,函数,类,逻辑:实现一个功能),本质是.py ...

  8. windows下安装python模块

    如何在windows下安装python模块 1. 官网下载安装包,比如(pip : https://pypi.python.org/pypi/pip#downloads) pip-9.0.1.tar. ...

  9. 安装第三方Python模块,增加InfoPi的健壮性

    这3个第三方Python模块是可选的,不安装的话InfoPi也可以运行. 但是如果安装了,会增加InfoPi的健壮性. 目录 1.cchardet    自动检测文本编码 2.lxml    用于解析 ...

随机推荐

  1. Python集合模块collections

    collections collections是Python内建的一个集合模块,提供了许多有用的集合类. namedtuple 我们知道tuple可以表示不变集合,例如,一个点的二维坐标就可以表示成: ...

  2. 全景分割panopticapi使用

    文件解析 参考github:https://github.com/cocodataset/panopticapi 输入图像:

  3. ASP.NET MVC2之Model Binder

    Model Binder在Asp.net MVC中非常简单.简单的说就是你控制器中的Action方法需要参数数据:而这些参数数据包含在HTTP请求中,包括表单上的Value和URL中的参 数等.而Mo ...

  4. impala 导出CSV 或excel

    1.介绍 impala-shell导入导出 参数说明: • -q query (--query=query) 从命令行执行查询,不进入impala-shell • -d default_db (--d ...

  5. laravel 开启sql调试

    打开app\Providers\AppServiceProvider.PHP,在boot方法中添加如下内容 public function boot() { //sql调试 $sql_debug = ...

  6. 理解MySQL数据库覆盖索引 (转)

    http://www.cnblogs.com/zl0372/articles/mysql_32.html 话说有这么一个表: CREATE TABLE `user_group` ( `id` int( ...

  7. mke2fs 制作ext2文件系统image

    方法1: 利用/dev/ram1: linux下有很多ram,我们用ram1,首先把ram1格式化成ext2文件系统[root@gucuiwen babylinux]#   sudo mkfs.ext ...

  8. 准确率,召回率,F值,机器学习分类问题的评价指标

    下面简单列举几种常用的推荐系统评测指标: 1.准确率与召回率(Precision & Recall) 准确率和召回率是广泛用于信息检索和统计学分类领域的两个度量值,用来评价结果的质量.其中精度 ...

  9. vi入门到精通

    VI是在Linux命令行下常用的文本编辑工具,在服务配置管理过程中经常用到:vi的常见的使用指南,互联网上随处可见,但仅能满足初学者对文档编辑的需求.这里就我自己在使用过程中通常用到的一些技巧操作方法 ...

  10. oracle sqlldr使用(导入速度快,但对数据本身的处理功能弱)

    oracle sqlldr使用(导入速度快,但对数据本身的处理功能弱) 博客分类: DB.Oracle OracleSQL  sqlload.cmd pause sqlldr user/pass@tn ...