python Python文件slurp
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1631897/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python File Slurp
提问by Mike Caron
Is there a one-liner to read all the lines of a file in Python, rather than the standard:
是否有一个单行来读取 Python 中文件的所有行,而不是标准:
f = open('x.txt')
cts = f.read()
f.close()
Seems like this is done so often that there's got to be a one-liner. Any ideas?
似乎这经常发生,以至于必须有一个单线。有任何想法吗?
回答by Alex Martelli
This will slurp the content into a single string in Python 2.61and above:
这将在 Python 2.6 1及更高版本中将内容放入单个字符串中:
with open('x.txt') as x: f = x.read()
And this will create a list of lines:
这将创建一个行列表:
with open('x.txt') as x: f = x.readlines()
These approaches guarantee immediate closure of the input file right after the reading.
这些方法保证在读取后立即关闭输入文件。
Footnote:
脚注:
- This approach can also be used in Python 2.5 using
from __future__ import with_statement
.
- 这种方法也可以在 Python 2.5 中使用
from __future__ import with_statement
。
An older approach that does notguarantee immediate closure is to use this to create a single string:
不保证立即关闭的旧方法是使用它来创建单个字符串:
f = open('x.txt').read()
And this to create a list of lines:
这将创建一个行列表:
f = open('x.txt').readlines()
In practice it will be immediately closed in some versions of CPython, but closed "only when the garbage collector gets around to it" in Jython, IronPython, and probably some future version of CPython.
在实践中,它会在某些版本的 CPython 中立即关闭,但在 Jython、IronPython 和可能的某些未来版本的 CPython 中“仅在垃圾收集器接近它时”关闭。
回答by Lutz Prechelt
If you are on Python3, make sure you properly respect your file's input encoding, e.g.:
如果您使用的是 Python3,请确保正确遵守文件的输入编码,例如:
import codecs
with codecs.open(filename, 'r', encoding="utf8") as file:
cts = file.read()
Find the list of codec names in the Python3 codec list. (The mechanism is also advisable for Python2 whenever you expect any non-ASCII input)
在Python3 编解码器列表中查找编解码器名称列表。(当您期望任何非 ASCII 输入时,该机制也适用于 Python2)
回答by drhagen
Starting in Python 3.5, you can use the pathlib
module for a more modern interface. Being Python 3, it makes a distinction between reading text and reading bytes:
从 Python 3.5 开始,您可以将该pathlib
模块用于更现代的界面。作为 Python 3,它区分了读取文本和读取字节:
from pathlib import Path
text_string = Path('x.txt').read_text() # type: str
byte_string = Path('x.txt').read_bytes() # type: bytes