Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

a = open("file", "r"); a.readline() output without \n [duplicate]

Tags:

python

I am about to write a python script, that is able to read a txt file, but with readline() there is always the \n output. How can i remove this from the variable ?

a = open("file", "r") b = a.readline() a.close() 
like image 517
Simon l. Avatar asked Oct 07 '16 15:10

Simon l.


People also ask

Does readline () take in the \n at the end of line?

In addition to the for loop, Python provides three methods to read data from the input file. The readline method reads one line from the file and returns it as a string. The string returned by readline will contain the newline character at the end.

What does readline () method return?

The readline() method returns one line from the file.

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.

Does readline return \n?

Python readline() method reads only one complete line from the file given. It appends a newline (“\n”) at the end of the line. If you open the file in normal read mode, readline() will return you the string.


2 Answers

That would be:

b.rstrip('\n') 

If you want to strip space from each and every line, you might consider instead:

a.read().splitlines() 

This will give you a list of lines, without the line end characters.

like image 126
wim Avatar answered Sep 27 '22 23:09

wim


A solution, can be:

with open("file", "r") as fd:     lines = fd.read().splitlines() 

You get the list of lines without "\r\n" or "\n".

Or, use the classic way:

with open("file", "r") as fd:     for line in fd:         line = line.strip() 

You read the file, line by line and drop the spaces and newlines.

If you only want to drop the newlines:

with open("file", "r") as fd:     for line in fd:         line = line.replace("\r", "").replace("\n", "") 

Et voilà.

Note: The behavior of Python 3 is a little different. To mimic this behavior, use io.open.

See the documentation of io.open.

So, you can use:

with io.open("file", "r", newline=None) as fd:     for line in fd:         line = line.replace("\n", "") 

When the newline parameter is None: lines in the input can end in '\n', '\r', or '\r\n', and these are translated into '\n'.

newline controls how universal newlines works (it only applies to text mode). It can be None, '', '\n', '\r', and '\r\n'. It works as follows:

On input, if newline is None, universal newlines mode is enabled. Lines in the input can end in '\n', '\r', or '\r\n', and these are translated into '\n' before being returned to the caller. If it is '', universal newlines mode is enabled, but line endings are returned to the caller untranslated. If it has any of the other legal values, input lines are only terminated by the given string, and the line ending is returned to the caller untranslated.

like image 24
Laurent LAPORTE Avatar answered Sep 28 '22 00:09

Laurent LAPORTE