python删除unicode和`\r\n`字符

python删除unicode和`\r\n`字符,python,Python,我想删除字符串中的“\r\n”字符。为此,我尝试了以下方法: s1.translate(dict.fromkeys(map(ord, u"\n\r"))) lists1=[] lists1.append(s1) print lists1 我收到这封信: [u'\r\nFoo\r\nBar, FooBar'] 如何删除字符串中的\r\n字符 cleaned = u"".join([line.strip() for line in u'\r\nFoo\r\nBar, Fo

我想删除字符串中的“\r\n”字符。为此,我尝试了以下方法:

s1.translate(dict.fromkeys(map(ord, u"\n\r")))
    lists1=[]
    lists1.append(s1)
    print lists1
我收到这封信:

[u'\r\nFoo\r\nBar, FooBar']
如何删除字符串中的
\r\n
字符

cleaned = u"".join([line.strip() for line in u'\r\nFoo\r\nBar, FooBar'.split("\r\n")])
或者只需使用
replace()

使用
str()
replace()
删除
u
\r\n

In [21]: strs = u'\r\nFoo\r\nBar'

In [22]: str(strs).replace("\r\n","")
Out[22]: 'FooBar'
In [23]: strs.replace("\r\n","")
Out[23]: u'FooBar'
或者只需
replace()
即可摆脱
\r\n

In [21]: strs = u'\r\nFoo\r\nBar'

In [22]: str(strs).replace("\r\n","")
Out[22]: 'FooBar'
In [23]: strs.replace("\r\n","")
Out[23]: u'FooBar'
你可以

'this is my string\r\nand here it continues'.replace('\r\n', '')

我认为你不需要先转换成字符串。OP说他也想去掉
u
(在标题中)哦,我明白了。但是这实际上涉及到一个编码,这里隐式地对“ascii”进行编码。但是OP以unicode文字开头。谢谢,文本中“\n”和“\r”分开的情况如何。在这种情况下,如何去掉“\n”和“\r”?@JohnSmith然后需要两个替换调用:
strs.replace(“\r”,”).replace(“\n”,”)
什么是“去掉unicode”?谢谢,字符串中“\n”和“\r”分开的情况如何。在这种情况下,如何去掉“\n”和“\r”?