Differences between revisions 5 and 6
Revision 5 as of 2004-09-19 22:34:09
Size: 4281
Editor: 61
Comment:
Revision 6 as of 2004-09-19 22:40:16
Size: 4317
Editor: 61
Comment:
Deletions are marked like this. Additions are marked like this.
Line 26: Line 26:
  更好的方法是将文件对象和一个变量绑定,可以及时关闭文件。比如,读取文本文件内容: 更好的方法是将文件对象和一个变量绑定,可以及时关闭文件。比如,读取文本文件内容:
Line 34: Line 34:
  将文本文件的全部内容按照分行 作为一个tuple读出有4种方法: 将文本文件的全部内容按照分行 作为一个tuple读出有4种方法:
Line 43: Line 43:
  方法1、2 返回的tuple中包含的string 元素末尾有'\n',
  方法3、4返回的tuple中包含的string元素末尾去掉了‘\n’.
方法1、2 返回的tuple中包含的string 元素末尾有'\n',
方法3、4返回的tuple中包含的string元素末尾去掉了‘\n’.
Line 46: Line 46:
  方法1是效率最高的,而且最符合python的风格。 方法1是效率最高的,而且最符合python的风格。
Line 48: Line 48:
  在Python 2.2以及更高版本中,有方法5和方法1等效 在Python 2.2以及更高版本中,有方法5和方法1等效
Line 59: Line 59:
如果文件大小不是特别大,那么一次将文件内容全部读出对于以后文件内容的处理是最快的,一般来说也是最方便的 如果文件大小不是特别大,那么一次将文件内容全部读出是最快的,一般来说对于以后文件内容的处理也是最方便的.

内置的打开文件函数是 '''open'''

文章来自《Python cookbook》.

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

-- 61.182.251.99 [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 返回的tuple中包含的string 元素末尾有'\n', 方法3、4返回的tuple中包含的string元素末尾去掉了‘\n’.

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

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

list_of_all_the_lines = list(file_object)                     # 方法 5 

   1 

讨论 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. 如果文件大小不是特别大,那么一次将文件内容全部读出是最快的,一般来说对于以后文件内容的处理也是最方便的.

内置的打开文件函数是 open

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.

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. ...

参考 See Also

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