-1

I've been attempting to strip out the \n plus the whitespace before and after the words from a string, but it is not working for some reason.

This is what I tried:

.strip(my_string)

and

re.sub('\n', '', my string)

I have tried using .strip and re in order to get it working, but it simply returns the same string.

Example input:

\\n                    The people who steal our cards already know all of this...\\n
          \\n                    , \\n                    I\'m sure every fraud minded person in America is taking notes.\\n
            \\n                  

Expected output would be:

The people who steal our cards already know all of this..., I\'m sure every fraud minded person in America is taking notes.
Bill Lynch
  • 80,138
  • 16
  • 128
  • 173
maudulus
  • 10,627
  • 10
  • 78
  • 117

1 Answers1

2

You're probably looking for something like this:

re.sub(r'\s+', r' ', x)

A usage example follows:

In [10]: x
Out[10]: 'hello \n world   \n blue'

In [11]: re.sub(r'\s+', r' ', x)
Out[11]: 'hello world blue'

If you'd also like to grab the sequence of characters r'\n', then let's grab them as well:

 re.sub(r'(\s|\\n)+', r' ', x)

And the output:

In [14]: x
Out[14]: 'hello \\n world  \n  \\n blue'

In [15]: re.sub(r'(\s|\\n)+', r' ', x)
Out[15]: 'hello world blue'
Bill Lynch
  • 80,138
  • 16
  • 128
  • 173
  • That almost works, in that it removes the white space, but the \\n is still there. – maudulus Aug 04 '14 at 16:22
  • `\\n The people who steal our cards already know all of this...\\n \\n , \\n I\'m sure every fraud minded person in America is taking notes.` is what I'm getting – maudulus Aug 04 '14 at 16:25
  • Note that `'\\n'` is a string of two characters (a backslash and a n) while `'\n'` is a single character (a newline character). That being said, I've edited my answer. – Bill Lynch Aug 04 '14 at 16:28