Best way to read delimited newlines in Python and drop newlines?

I am trying to determine the best way to deal with getting rid of newlines when reading in newline-delimited files in Python.

What I came up with is the following code that includes throwaway code for testing.

import os def getfile(filename,results): f = open(filename) filecontents = f.readlines() for line in filecontents: foo = line.strip('\n') results.append(foo) return results blahblah = [] getfile('/tmp/foo',blahblah) for x in blahblah: print x 

Suggestions?

+73
python file readline
Feb 13 '09 at 6:31
source share
7 answers
 lines = open(filename).read().splitlines() 
+176
Feb 13 '09 at 6:35
source share

Here, the generator does what you requested. In this case, using rstrip is sufficient and slightly faster than the strip.

 lines = (line.rstrip('\n') for line in open(filename)) 

However, you most likely want to use this to get rid of lagging spaces.

 lines = (line.rstrip() for line in open(filename)) 
+20
Feb 13 '09 at 8:35
source share
 for line in file('/tmp/foo'): print line.strip('\n') 
+8
Feb 13 '09 at 6:36
source share

What do you think of this approach?

 with open(filename) as data: datalines = (line.rstrip('\r\n') for line in data) for line in datalines: ...do something awesome... 

The expression of the generator avoids loading the entire file into memory, and with ensures closing the file

+8
Aug 08 2018-11-11T00:
source share

Just use generator expressions:

 blahblah = (l.rstrip() for l in open(filename)) for x in blahblah: print x 

I also want to advise you not to read the entire file in memory - a cycle with generators is much more efficient on large data sets.

+4
Feb 14 '09 at 7:43
source share

I use this

 def cleaned( aFile ): for line in aFile: yield line.strip() 

Then I can do such things.

 lines = list( cleaned( open("file","r") ) ) 

Or, I can extend the cleaning with additional functions, for example, discard empty lines or skip comment lines or something else.

+3
Feb 13 '09 at 11:07
source share

I would do it like this:

 f = open('test.txt') l = [l for l in f.readlines() if l.strip()] f.close() print l 
+2
Feb 13 '09 at 6:43
source share



All Articles