Is there a short collapse library function in python?

Is there a cross-platform library that collapses a multiline string into a single-line string without repeating spaces?

I came up with some kind of snippet, but I wonder if there is a standard function that I could import, maybe even optimized in C?

def collapse(input): import re rn = re.compile(r'(\r\n)+') r = re.compile(r'\r+') n = re.compile(r'\n+') s = re.compile(r'\ +') return s.sub(' ',n.sub(' ',r.sub(' ',rn.sub(' ',input)))) 

PS Thanks for the good observations. ' '.join(input.split()) seems to be a winner, since it actually works about twice as fast in my case as compared to search-replace with the pre-compiled regular expression r'\s+' .

+4
source share
3 answers

The built-in string.split() method will be split into spaces, so you can use it and then join the resulting list with spaces, for example:

 ' '.join(my_string.split()) 

Here's the full test script:

 TEST = """This is a test\twith a mix of\ttabs, newlines and repeating whitespace""" print ' '.join(TEST.split()) # Prints: # This is a test with a mix of tabs, newlines and repeating whitespace 
+12
source

You had the right idea, you just needed to read the python manual a bit:

 import re somewhitespace = re.compile(r'\s+') TEST = """This is a test\twith a mix of\ttabs, newlines and repeating whitespace""" somewhitespace.sub(' ', TEST) 'This is a test with a mix of tabs, newlines and repeating whitespace' 
+4
source
 multi_line.replace('\n', '') 

will do the job. '\n' is the universal end of line character in python.

0
source

All Articles