问题:如何读取没有换行符的文件?

在Python中,调用

temp = open(filename,'r').readlines()

产生一个列表,其中每个元素都是文件中的一行。这有点愚蠢,但是仍然:readlines()还向每个元素写入换行符,这是我不希望发生的事情。

我该如何避免呢?

In Python, calling

temp = open(filename,'r').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.

How can I avoid it?


回答 0

您可以使用读取整个文件并分割行str.splitlines

temp = file.read().splitlines()

或者,您可以手动删除换行符:

temp = [line[:-1] for line in 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换行符:

[line.rstrip('\n') for line in file]

甚至,尽管很难理解:

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

这利用了以下事实:的返回值or不是布尔值,而是被评估为true或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()保留它。

注意:为了readlines()使writelines()方法对称,不会添加结尾换行符,因此f2.writelines(f.readlines())会生成fin 的精确副本f2

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]

Or a simpler alternative is to strip the newline instead:

[line.rstrip('\n') for line in file]

Or even, although pretty unreadable:

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

Which exploits the fact that the return value of or isn’t a boolean, but the object that was evaluated true or false.


The readlines method is actually equivalent to:

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

Since readline() keeps the newline also readlines() keeps it.

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.


回答 1

temp = open(filename,'r').read().split('\n')
temp = open(filename,'r').read().split('\n')

回答 2

另一个例子:

一次读取文件。从字符串结尾删除不需要的字符str.rstrip(chars)

with open(filename, 'r') as fileobj:
    for row in fileobj:
        print( row.rstrip('\n') )

又见str.strip([chars])str.lstrip([chars])

(python> = 2.0)

another example:

Reading file one row at the time. Removing unwanted chars with from end of the string str.rstrip(chars)

with open(filename, 'r') as fileobj:
    for row in fileobj:
        print( row.rstrip('\n') )

see also str.strip([chars]) and str.lstrip([chars])

(python >= 2.0)


回答 3

temp = open(filename,'r').read().splitlines()
temp = open(filename,'r').read().splitlines()

回答 4

我认为这是最好的选择。

temp = [line.strip() for line in file.readlines()]

I think this is the best option.

temp = [line.strip() for line in file.readlines()]

回答 5

尝试这个:

u=open("url.txt","r")  
url=u.read().replace('\n','')  
print(url)  

Try this:

u=open("url.txt","r")  
url=u.read().replace('\n','')  
print(url)  

回答 6

my_file = open("first_file.txt", "r")
for line in my_file.readlines():
    if line[-1:] == "\n":
        print(line[:-1])
    else:
        print(line)
my_file.close() 
my_file = open("first_file.txt", "r")
for line in my_file.readlines():
    if line[-1:] == "\n":
        print(line[:-1])
    else:
        print(line)
my_file.close() 

回答 7

import csv

with open(filename) as f:
    csvreader = csv.reader(f)
    for line in csvreader:
         print(line[0])
import csv

with open(filename) as f:
    csvreader = csv.reader(f)
    for line in csvreader:
         print(line[0])

回答 8

def getText():
    file=open("ex1.txt","r");

    names=file.read().split("\n");
    for x,word in enumerate(names):
        if(len(word)>=20):
            return 0;
            print "length of ",word,"is over 20"
            break;
        if(x==20):
            return 0;
            break;
    else:
        return names;


def show(names):
    for word in names:
        len_set=len(set(word))
        print word," ",len_set


for i in range(1):

    names=getText();
    if(names!=0):
        show(names);
    else:
        break;
def getText():
    file=open("ex1.txt","r");

    names=file.read().split("\n");
    for x,word in enumerate(names):
        if(len(word)>=20):
            return 0;
            print "length of ",word,"is over 20"
            break;
        if(x==20):
            return 0;
            break;
    else:
        return names;


def show(names):
    for word in names:
        len_set=len(set(word))
        print word," ",len_set


for i in range(1):

    names=getText();
    if(names!=0):
        show(names);
    else:
        break;

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。