如何读取没有换行符的文件? [英] How to read a file without newlines?

查看:126
本文介绍了如何读取没有换行符的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我该如何避免呢?

推荐答案

您可以使用

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) != '\n':
        # add missing newline if not already present
        f.write('\n')
        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('\n') for line in file]

甚至,尽管很难理解:

[line[:-(line[-1] == '\n') or len(line)+1] for line in file]

利用以下事实:or的返回值不是布尔值,而是被评估为true或false的对象.

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.

注意:对于 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆