Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read a file without newlines?

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?

like image 588
Yotam Avatar asked Sep 08 '12 11:09

Yotam


People also ask

How do you get rid of n When reading a file in Python?

. rstrip('\r\n') , or simply . rstrip() , would strip both.

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.

How do you read a new line character in Python?

Method 1: Read a File Line by Line using readlines() This function can be used for small files, as it reads the whole file content to the memory, then split it into separate lines. We can iterate over the list and strip the newline '\n' character using strip() function. Example: Python3.

Does readline read newline Python?

Since readline() keeps the newline also readlines() keeps it. Note: for symmetry to readlines() the writelines() method does not add ending newlines, so f2.


2 Answers

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.

like image 67
Bakuriu Avatar answered Oct 06 '22 02:10

Bakuriu


temp = open(filename,'r').read().splitlines() 
like image 25
vivek Avatar answered Oct 06 '22 03:10

vivek