0

I've looked at previous answers to this (python how to remove this n from string or list, remove a list item from a list and python remove whitespace in string) but can't get the solutions to work.

I have a list with a single element like so:

list = [u'\r\n\r\n\r\n            \r\n                \r\n                    \r\n                    123 Main St., Peoria\r\n                \r\n\r\n            \r\n             |\r\n             \r\n                    \r\n                        \r\n                            \r\n                            123-456-789\r\n                        \r\n                    \r\n            \r\n        ']

It has an address and a phone number and what I'd like is just to have this returned is:

123 Main St., Peoria;123-456-789

I've tried stuff like:

str(list).strip(' \r\n\t')

and

str(list).replace('\r','')

But they don't work, so I'm thinking maybe it's a unicode issue? How do I get around it?

Community
  • 1
  • 1
eamon1234
  • 1,555
  • 3
  • 19
  • 38

2 Answers2

5

Just take the one element out of the list and replace on there:

print lst[0].replace('\r', '').replace('\n', '')

There is no need to convert the list itself to string here.

You could, in this case, also combine unicode.strip with a .splitlines() to remove whitespace from each line, then rejoin:

print u' '.join([l.strip() for l in lst[0].splitlines() if l.strip()])

This prints:

123 Main St., Peoria | 123-456-789
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
3
import re

li = [u'\r\n\r\n\r\n \r\n \r\n \r\n 123 Main St., Peoria\r\n \r\n\r\n \r\n |\r\n \r\n \r\n \r\n \r\n 123-456-789\r\n \r\n \r\n \r\n ']
print re.sub(r'\s+', ' ', li[0].replace(' |', ';'))

prints

123 Main St., Peoria; 123-456-789
eumiro
  • 207,213
  • 34
  • 299
  • 261