Differences between revisions 5 and 17 (spanning 12 versions)
Revision 5 as of 2004-09-19 22:34:09
Size: 4281
Editor: 61
Comment:
Revision 17 as of 2004-09-20 01:20:31
Size: 5310
Editor: 61
Comment:
Deletions are marked like this. Additions are marked like this.
Line 8: Line 8:
-- 61.182.251.99 [[[DateTime(2004-09-19T22:05:29Z)]]] -- v_gyc@yahoo.com [[[DateTime(2004-09-19T22:05:29Z)]]]
Line 12: Line 12:
Line 26: Line 27:
  更好的方法是将文件对象和一个变量绑定,可以及时关闭文件。比如,读取文本文件内容: 更好的方法是将文件对象和一个变量绑定,可以及时关闭文件。比如,读取文本文件内容:
Line 34: Line 35:
  将文本文件的全部内容按照分行 作为一个tuple读出有4种方法: 将文本文件的全部内容按照分行 作为一个tuple读出有4种方法:
Line 43: Line 44:
  方法1、2 返回的tuple中包含的string 元素末尾有'\n',
  方法3、4返回的tuple中包含的string元素末尾去掉了‘\n’.
方法1、2 返回的list中包含的string 元素末尾有'\n',
方法3、4 返回的list中包含的string元素末尾去掉了‘\n’.
Line 46: Line 47:
  方法1是效率最高的,而且最符合python的风格。 方法1是效率最高的,而且最符合python的风格。
Line 48: Line 49:
  在Python 2.2以及更高版本中,有方法5和方法1等效 在Python 2.2以及更高版本中,有方法5和方法1等效
Line 53: Line 54:
== 讨论 ==

如果文件大小不是特别大,那么一次将文件内容全部读出是最快的,一般来说对于以后文件内容的处理也是最方便的。

内置函数 '''open''',打开文件并返回一个文件对象实例。 可以调用'''read'''方法读取文件内容(文本或2进制)到一个大字符串。

如果是文本内容,可以使用'''string'''的'''split'''方法或者特别的'''splitlines'''方法将此string分解成文本行的'''list'''。

经常进行文件内容按行分解,可以直接调用文件对象的'''readlines'''方法,更方便更快捷。:)。

在Python 2.2以及更高版本中,可以直接将文件对象实例作为内置'''list'''函数的唯一参数。




在Unix 以及类Unix系统,比如Linux和BSD的各种变体中,文本文件和2进制文件没有实质的区别。

在Windows和Macintosh系统中,文本文件中的分行符不是标准的'\n'分隔符,分别是'\r\n'和'\r'。

打开文本文件时,Python会自动用'\n'替换相应的分隔符。

因此, 打开2进制文件不需要这种自动替换时,必须通知Python:
      open打开文件时第2个参数使用'''rb'''。

在类Unix平台上使用'''rb'''参数也没有什么坏处。 虽然不是必须的,但是区分文本文件和2进制文件是一个好习惯,可以使程序易于理解,同时具有较好的跨平台性。


你可以直接在'''open'''函数获得的文件对象上调用文件的方法比如read(象第一个程序片断中).当方法调用结束时,你不再拥有被打开文件对象的引用。 实际上,Python 检测到这个文件对象没有被引用,会马上关闭这个文件对象。然而,更佳的做法是持有一个open返回的文件对象的引用,这样当结束处理文件时,可以自己显式关闭文件。这可以保证文件被打开的时间尽可能的短,即使在Jython和以后的Python版本中由于拥有更先进的垃圾回收机制可能延缓Python中的自动关闭文件功能时,自己关闭文件也是有效的。


如果不是需要读取全部文件内容,而是每次读取文件的部分内容,习惯方法是不同的。

下面的程序片断显示了如何从文件中每次读取100字节,一直到文件末尾:
Line 54: Line 89:
#!python
Line 56: Line 90:
file_object = open('abinfile', 'rb')
while 1:
    chunk = file_object.read(100)
    if not chunk: break
    do_something_with(chunk)
file_object.close( )
Line 57: Line 97:
== 讨论 Discussion ==
Unless the file you're reading is truly huge, slurping it all into memory in one gulp is fastest and generally most convenient for any further processing. The built-in function open creates a Python file object. With that object, you call the read method to get all of the contents (whether text or binary) as a single large string. If the contents are text, you may choose to immediately split that string into a list of lines, with the split method or with the specialized splitlines method. Since such splitting is a frequent need, you may also call readlines directly on the file object, for slightly faster and more convenient operation. In Python 2.2, you can also pass the file object directly as the only argument to the built-in type list.
如果文件大小不是特别大,那么一次将文件内容全部读出对于以后文件内容的处理是最快的,一般来说也是最方便的
Passing an argument N to the read method ensures that read will read only the next N bytes (or fewer, if the file is closer to the end). read returns the empty string when it reaches the end of the file.
Line 61: Line 99:
On Unix and Unix-like systems, such as Linux and BSD variants, there is no real distinction between text files and binary data files. On Windows and Macintosh systems, however, line terminators in text files are encoded not with the standard '\n' separator, but with '\r\n' and '\r', respectively. Python translates the line-termination characters into '\n' on your behalf, but this means that you need to tell Python when you open a binary file, so that it won't perform the translation. To do that, use 'rb' as the second argument to open. This is innocuous even on Unix-like platforms, and it's a good habit to distinguish binary files from text files even there, although it's not mandatory in that case. Such a good habit will make your programs more directly understandable, as well as letting you move them between platforms more easily. Reading a text file one line at a time is a frequent task. In Python 2.2 and later, this is the easiest, clearest, and fastest approach:
Line 63: Line 101:
You can call methods such as read directly on the file object produced by the open function, as shown in the first snippet of the solution. When you do this, as soon as the reading operation finishes, you no longer have a reference to the file object. In practice, Python notices the lack of a reference at once and immediately closes the file. However, it is better to bind a name to the result of open, so that you can call close yourself explicitly when you are done with the file. This ensures that the file stays open for as short a time as possible, even on platforms such as Jython and hypothetical future versions of Python on which more advanced garbage-collection mechanisms might delay the automatic closing that Python performs.
...
for line in open('thefile.txt'):
    do_something_with(line)
Several idioms were common in older versions of Python. The one idiom you can be sure will work even on extremely old versions of Python, such as 1.5.2, is quite similar to the idiom for reading a binary file a chunk at a time:

file_object = open('thefile.txt')
while 1:
    line = file_object.readline( )
    if not line: break
    do_something_with(line)
file_object.close( )
readline, like read, returns the empty string when it reaches the end of the file. Note that the end of the file is easily distinguished from an empty line because the latter is returned by readline as '\n', which is not an empty string but rather a string with a length of 1.

文章来自《Python cookbook》.

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

-- [email protected] [DateTime(2004-09-19T22:05:29Z)] TableOfContents

描述

读取文件内容

Credit: Luther Blissett

问题

从文件读取文本或数据

解决

一次将文件内容读入一个长字符串的最简便方法

all_the_text = open('thefile.txt').read(  )    # 文本文件的全部文本
all_the_data = open('abinfile', 'rb').read(  ) # 2进制文件的全部数据

更好的方法是将文件对象和一个变量绑定,可以及时关闭文件。比如,读取文本文件内容:

file_object = open('thefile.txt')              # 打开文件
all_the_text = file_object.read(  )            # 文本文件的全部文本
file_object.close(  )                          # 使用完毕,关闭文件

将文本文件的全部内容按照分行 作为一个tuple读出有4种方法:

list_of_all_the_lines = file_object.readlines(  )             # 方法 1
list_of_all_the_lines = file_object.read(  ).splitlines(1)    # 方法 2
list_of_all_the_lines = file_object.read().splitlines(  )     # 方法 3 
list_of_all_the_lines = file_object.read(  ).split('\n')      # 方法 4 

方法1、2 返回的list中包含的string 元素末尾有'\n', 方法3、4 返回的list中包含的string元素末尾去掉了‘\n’.

方法1是效率最高的,而且最符合python的风格。

在Python 2.2以及更高版本中,有方法5和方法1等效

list_of_all_the_lines = list(file_object)                     # 方法 5 

讨论

如果文件大小不是特别大,那么一次将文件内容全部读出是最快的,一般来说对于以后文件内容的处理也是最方便的。

内置函数 open,打开文件并返回一个文件对象实例。 可以调用read方法读取文件内容(文本或2进制)到一个大字符串。

如果是文本内容,可以使用stringsplit方法或者特别的splitlines方法将此string分解成文本行的list

经常进行文件内容按行分解,可以直接调用文件对象的readlines方法,更方便更快捷。:)。

在Python 2.2以及更高版本中,可以直接将文件对象实例作为内置list函数的唯一参数。

在Unix 以及类Unix系统,比如Linux和BSD的各种变体中,文本文件和2进制文件没有实质的区别。

在Windows和Macintosh系统中,文本文件中的分行符不是标准的'\n'分隔符,分别是'\r\n'和'\r'。

打开文本文件时,Python会自动用'\n'替换相应的分隔符。

因此, 打开2进制文件不需要这种自动替换时,必须通知Python:

  • open打开文件时第2个参数使用rb

在类Unix平台上使用rb参数也没有什么坏处。 虽然不是必须的,但是区分文本文件和2进制文件是一个好习惯,可以使程序易于理解,同时具有较好的跨平台性。

你可以直接在open函数获得的文件对象上调用文件的方法比如read(象第一个程序片断中).当方法调用结束时,你不再拥有被打开文件对象的引用。 实际上,Python 检测到这个文件对象没有被引用,会马上关闭这个文件对象。然而,更佳的做法是持有一个open返回的文件对象的引用,这样当结束处理文件时,可以自己显式关闭文件。这可以保证文件被打开的时间尽可能的短,即使在Jython和以后的Python版本中由于拥有更先进的垃圾回收机制可能延缓Python中的自动关闭文件功能时,自己关闭文件也是有效的。

如果不是需要读取全部文件内容,而是每次读取文件的部分内容,习惯方法是不同的。

下面的程序片断显示了如何从文件中每次读取100字节,一直到文件末尾:

file_object = open('abinfile', 'rb')
while 1:
    chunk = file_object.read(100)
    if not chunk: break
    do_something_with(chunk)
file_object.close(  )

Passing an argument N to the read method ensures that read will read only the next N bytes (or fewer, if the file is closer to the end). read returns the empty string when it reaches the end of the file.

Reading a text file one line at a time is a frequent task. In Python 2.2 and later, this is the easiest, clearest, and fastest approach:

for line in open('thefile.txt'):

  • do_something_with(line)

Several idioms were common in older versions of Python. The one idiom you can be sure will work even on extremely old versions of Python, such as 1.5.2, is quite similar to the idiom for reading a binary file a chunk at a time:

file_object = open('thefile.txt') while 1:

  • line = file_object.readline( ) if not line: break do_something_with(line)

file_object.close( ) readline, like read, returns the empty string when it reaches the end of the file. Note that the end of the file is easily distinguished from an empty line because the latter is returned by readline as '\n', which is not an empty string but rather a string with a length of 1.

参考 See Also

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