Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

file.readlines leaving blank lines [duplicate]

I have read that file.readlines reads the whole file line by line and stores it in a list. If I have a file like so -

Sentence 1
Sentence 2
Sentence 3

and I use readlines to print each sentence like so -

file = open("test.txt") 
for i in file.readlines():
    print i

The output is

Sentence 1

Sentence 2

Sentence 3

My question is why do I get the extra line between each sentence and how can I get rid of it?

UPDATE

I found that using i.strip also removes the extra lines. Why does this happen? As far as I know, split removes the white spaces at the end and beginning of a string.

like image 680
MayankJain Avatar asked Nov 04 '13 06:11

MayankJain


People also ask

How do I strip a new line in Readlines?

If you want to strip the newline character \n from each line when adding it to a list you can use the strip() method within a list comprehension: with open('file. txt') as f: lines = [ line.

What does file Readlines () do?

The readlines() method returns a list containing each line in the file as a list item. Use the hint parameter to limit the number of lines returned.

How readline () is different from Readlines ()?

What is Python readline()? Python readline() method will return a line from the file when called. readlines() method will return all the lines in a file in the format of a list where each element is a line in the file.


1 Answers

file.readlines() return list of strings. Each string contain trailing newlines. print statement prints the passed parameter with newlnie.; That's why you got extra lines.

To remove extra newline, use str.rstrip:

print i.rstrip('\n')

or use sys.stdout.write

sys.stdout.write(i)

BTW, don't use file.readlines unless you need all lines at once. Just iterate the file.

with open("test.txt") as f:
    for i in f:
        print i.rstrip('\n')
        ...

UPDATE

In Python 3, to prevent print prints trailing newline, you can use print(i, end='').

In Python 2, you can use same feature if you do : from __future__ import print_function

Answer to UPDATE

Tabs, Newlines are also considers as whitespaces.

>> ' \r\n\t\v'.isspace()
True
like image 136
falsetru Avatar answered Sep 20 '22 03:09

falsetru