如何在没有换行符的情况下读取文件? [英] How to read a file without newlines?
问题描述
在 Python 中,调用
In Python, calling
temp = open(filename,'r').readlines()
生成一个列表,其中每个元素都是文件中的一行.这有点愚蠢,但仍然: readlines()
还向每个元素写入换行符,这是我不希望发生的.
results in a list in which each element is a line in the file. It's a little stupid but still: readlines()
also writes newline character to each element, something I do not wish to happen.
我怎样才能避免它?
推荐答案
您可以使用 str.splitlines
:
You can read the whole file and split lines using str.splitlines
:
temp = file.read().splitlines()
或者您可以手动去除换行符:
Or you can strip the newline by hand:
temp = [line[:-1] for line in file]
注意:最后一个解决方案仅适用于文件以换行符结尾的情况,否则最后一行将丢失一个字符.
Note: this last solution only works if the file ends with a newline, otherwise the last line will lose a character.
这个假设在大多数情况下都是正确的(特别是对于由文本编辑器创建的文件,无论如何它们通常会添加结束换行符).
This assumption is true in most cases (especially for files created by text editors, which often do add an ending newline anyway).
如果你想避免这种情况,你可以在文件末尾添加一个换行符:
If you want to avoid this you can add a newline at the end of file:
with open(the_file, 'r+') as f:
f.seek(-1, 2) # go at the end of the file
if f.read(1) != '
':
# add missing newline if not already present
f.write('
')
f.flush()
f.seek(0)
lines = [line[:-1] for line in f]
或者更简单的替代方法是 strip
换行:
Or a simpler alternative is to strip
the newline instead:
[line.rstrip('
') for line in file]
甚至,虽然很难读:
[line[:-(line[-1] == '
') or len(line)+1] for line in file]
它利用了 or
的返回值不是布尔值,而是被评估为真或假的对象这一事实.
Which exploits the fact that the return value of or
isn't a boolean, but the object that was evaluated true or false.
readlines
方法实际上相当于:
def readlines(self):
lines = []
for line in iter(self.readline, ''):
lines.append(line)
return lines
# or equivalently
def readlines(self):
lines = []
while True:
line = self.readline()
if not line:
break
lines.append(line)
return lines
由于 readline()
保留了换行符,所以 readlines()
也保留了它.
Since readline()
keeps the newline also readlines()
keeps it.
注意:为了与 readlines()
对称,writelines()
方法不添加结束换行符,所以 f2.writelines(f.readlines())
在 f2
中生成 f
的精确副本.
Note: for symmetry to readlines()
the writelines()
method does not add ending newlines, so f2.writelines(f.readlines())
produces an exact copy of f
in f2
.
这篇关于如何在没有换行符的情况下读取文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!