2

I have a list that looks like this:

['\r1/19/2015', '1/25/2015\r']

I got this data from the web, and it just started coming with the \r today. This causes an error in my script, so I am wondering how to remove the \r from the list.

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
iqueqiorio
  • 1,149
  • 2
  • 35
  • 78

3 Answers3

7

You can use str.strip. '\r' is a carriage return, and strip removes leading and trailing whitespace and new line characters.

>>> l = ['\r1/19/2015', '1/25/2015\r']
>>> l = [i.strip() for i in l]
>>> l
['1/19/2015', '1/25/2015']
Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
4

You can use replace also

>>> l = ['\r1/19/2015', '1/25/2015\r']
>>> [i.replace('\r','') for i in l]
['1/19/2015', '1/25/2015']
Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
2

If for some reason the \r characters are not just at the beginning and/or end of your strings, the following will work:

In [77]: L = ['\r1/19/2015', '1/25/2015\r', '1/23/\r2015']

In [78]: [item.replace("\r", "") for item in L]
Out[78]: ['1/19/2015', '1/25/2015', '1/23/2015']
MattDMo
  • 100,794
  • 21
  • 241
  • 231