Differences between revisions 3 and 9 (spanning 6 versions)
Revision 3 as of 2004-09-20 22:38:41
Size: 2050
Editor: 61
Comment:
Revision 9 as of 2009-12-25 07:16:21
Size: 2497
Editor: localhost
Comment: converted to 1.6 markup
Deletions are marked like this. Additions are marked like this.
Line 8: Line 8:
-- 61.182.251.99 [[[DateTime(2004-09-20T22:17:38Z)]]]
[[TableOfContents]]
-- 61.182.251.99 [<<DateTime(2004-09-20T22:17:38Z)>>]
<<TableOfContents>>
Line 14: Line 14:
Line 18: Line 17:

4.5.2 Solution
Line 27: Line 24:
Line 28: Line 26:
#!python
Line 29: Line 28:
   #thefiepath 文件路径
   #desired_line_number 整数,文件的特定行
Line 30: Line 31:
Line 34: Line 36:
使用标准模块'''linecache'''处理这个任务,一般来说是最佳方法。由于lincecache缓存了文件的信息,当需要多次提取文件的某些特定行时,可以避免无用的重复操作。 使用标准模块'''linecache'''处理这个任务,一般来说是最佳 由于lincecache缓存了文件的信息,当需要多次提取文件的某些特定行时,可以避免无用的重复操作。
Line 36: Line 38:
如果不再需要读取文件特定行而且脚本需要继续运行,应该使用'''linecache'''的'''clearcache'''方法释放缓存占用的内存  如果不再需要读取文件特定行而且脚本需要继续运行,应该使用'''linecache'''的'''clearcache'''方法释放缓存。
Line 40: Line 42:
'''linecache'''读取并缓存作为参数传入的文件的全部信息。如果处理一个很大的文件,而且只需要读取某一个特定行,使用'''linecache'''可能会进行不是严格必须的操作。如果这个例程恰好是程序性能瓶颈,可以使用显示 reads and caches all of the text file whose name you pass to it, so if it's a very large file and you need only one of its lines, linecache may be doing more work than is strictly necessary. Should this happen to be a bottleneck for your program, you may get some speed-up by coding an explicit loop, encapsulated within a function. Here's how to do this in Python 2.2: '''linecache'''读取并缓存作为参数传入的文件的全部信息。 如果处理很大的文件,而且只需要读取某一个特定行,使用'''linecache'''可能会进行不是严格必须的操作。如果这个例程恰好是程序性能瓶颈,可以编写函数,封装循环代码,以获得性能提升。 Python 2.2中函数代码如下:
Line 42: Line 44:
{{{
#!python
Line 43: Line 47:
    if desired_line_number < 1: return ''
    current_line_number = 0
    for line in open(thefilepath):
        current_line_number += 1
        if current_line_number == desired_line_number: return line
    if desired_line_number < 1: return ''         
    current_line_number = 0                     
    for line in open(thefilepath):                 #译注:only in Python 2.2 and late
        current_line_number += 1                   
        if current_line_number == desired_line_number: return line     #译注: 返回特定行数据
Line 49: Line 53:
}}}
Line 50: Line 55:
... Python 2.1版本中只有少许不同,只是代码效率稍低,繁琐点。如下改变上面的''''for'''语句即可
{{{
#!python
         for line in open(thefilepath).xreadlines( ):
}}}
Line 52: Line 61:
在Python 2.0 以及更早版本中,没有提供如上面的仅使用限量内存,每次一行的提取大文件信息的功能。如果不得不使用这些版本, '''linecache'''可能是大多数情况下的较好选择。
 
Line 53: Line 64:

Python 文档'''linecache'''模块部分;

Perl Cookbook Recipe 8.8.

文章来自《Python cookbook》.

翻译仅仅是为了个人学习,其它商业版权纠纷与此无关!

-- 61.182.251.99 [2004-09-20 22:17:38]

描述

读取文件特定行

问题 Problem

读取文件的指定一行

解决 Solution

The standard linecache module makes this a snap:

使用标准模块linecache处理,很简单:

   1 import linecache
   2    #thefiepath             文件路径
   3    #desired_line_number    整数,文件的特定行 
   4 theline = linecache.getline(thefilepath, desired_line_number)

讨论 Discussion

使用标准模块linecache处理这个任务,一般来说是最佳的。 由于lincecache缓存了文件的信息,当需要多次提取文件的某些特定行时,可以避免无用的重复操作。

如果不再需要读取文件特定行而且脚本需要继续运行,应该使用linecacheclearcache方法释放缓存。

使用checkcache方法可以检查磁盘文件是否已经变化,如果变化可以更新缓存信息。

linecache读取并缓存作为参数传入的文件的全部信息。 如果处理很大的文件,而且只需要读取某一个特定行,使用linecache可能会进行不是严格必须的操作。如果这个例程恰好是程序性能瓶颈,可以编写函数,封装循环代码,以获得性能提升。 Python 2.2中函数代码如下:

   1 def getline(thefilepath, desired_line_number):
   2     if desired_line_number < 1: return ''         
   3     current_line_number = 0                     
   4     for line in open(thefilepath):                 #译注:only in Python 2.2 and late
   5         current_line_number += 1                   
   6         if current_line_number == desired_line_number: return line     #译注: 返回特定行数据
   7     return ''

Python 2.1版本中只有少许不同,只是代码效率稍低,繁琐点。如下改变上面的'for语句即可

   1          for line in open(thefilepath).xreadlines(  ):

在Python 2.0 以及更早版本中,没有提供如上面的仅使用限量内存,每次一行的提取大文件信息的功能。如果不得不使用这些版本, linecache可能是大多数情况下的较好选择。

参考 See Also

Python 文档linecache模块部分;

Perl Cookbook Recipe 8.8.

PyCkBk-4-5 (last edited 2009-12-25 07:16:21 by localhost)