Reputation: 11794
Do anyone know an alternative ways to force a string into one liner? May be a python built-in method? or perhaps a better implementation (explain why)?
For example:
Hello World
Foo Bar
Become
Hello World Foo Bar
def strip_newline(s):
return ' '.join(mystring.splitlines())
strip_newline = lambda _: ' '.join(_.splitlines())
For those who reminds me about character replacement of \n
, a line character into . The drawback is you need to watch out for
\r
, carriage return character too. :-)
Thus you need to do mystring.replace('\r', ' ').replace('\n', ' ')
.
Upvotes: 2
Views: 205
Reputation: 10951
Other approach would be using str.translate
method, by building the dictionary of translation (char_to_remove:None mapping) then applying it through str.translate
:
>>> import string
>>> string.whitespace
' \t\n\r\x0b\x0c'
>>> st
'\n1\n2\n3\n4\n5\n'
>>>
>>> dws = dict.fromkeys(ord(c) for c in string.whitespace)
>>>
>>> dws
{32: None, 9: None, 10: None, 11: None, 12: None, 13: None}
>>>
>>> st.translate(dws)
'12345'
Of course, this would remove the white space within your string, you can skip this case by conditional comprehension:
>>> dws = dict.fromkeys(ord(c) for c in string.whitespace if c != ' ')
Or simply:
>>> import os
>>> os.linesep
'\n' #on my ubuntu box
>>> st.translate({ord(os.linesep):None})
'12345'
Upvotes: 0
Reputation: 1401
Performance
I timed both methods in a bash shell, and it appears that the simple
stri.replace("\n", " ")
is faster than your proposed solution.
(The file hello.txt is a file containing 1000 lines with the phrase "Hello World".)
~$ time python -c 'f = open("hello.txt","r"); stri = f.read(); stri.replace("\n", " ")'
real 0m0.130s
user 0m0.021s
sys 0m0.023s
~$ time python -c 'f = open("hello.txt","r"); stri = f.read(); " ".join(stri.splitlines())'
real 0m0.317s
user 0m0.032s
sys 0m0.028s
Alternatively, if you use the readlines()
method instead of the read()
method, you can remove the need for your call to splitlines()
and gain some performance improvement there.
~$ time python -c 'f = open("hello.txt","r"); stri = f.readlines(); " ".join(stri)'
real 0m0.176s
user 0m0.033s
sys 0m0.026s
Line Endings
I'm using linux, so I don't have to worry about the \r
character. However, in your case, Windows line endings are always of the form \r\n
, so you can replace your two calls to the replace()
method
stri.replace("\r", " ").replace("\n", " ")
with a single call:
stri.replace("\r\n", " ")
which should also improve performance.
Upvotes: 2
Reputation: 2817
I think simple replace
would be the fastest method:
s = '''
1
2
3
4
5
'''
print (s.replace("\n", " "))
Upvotes: 3