[英]python get rid of unicode and `\r\n` characters
I want to get rid `\\r\\n' characters in my string to do so, I tried this : 我想摆脱字符串中的`\\ r \\ n'字符,我尝试这样做:
s1.translate(dict.fromkeys(map(ord, u"\n\r")))
lists1=[]
lists1.append(s1)
print lists1
I received this: 我收到了:
[u'\r\nFoo\r\nBar, FooBar']
How can I get rid of \\r\\n
characters in my string ? 如何摆脱字符串中的\\r\\n
字符?
Use str()
and replace()
to remove both u
and \\r\\n
: 使用str()
和replace()
删除u
和\\r\\n
:
In [21]: strs = u'\r\nFoo\r\nBar'
In [22]: str(strs).replace("\r\n","")
Out[22]: 'FooBar'
or just replace()
to get rid of only \\r\\n
: 或只是replace()
摆脱\\r\\n
:
In [23]: strs.replace("\r\n","")
Out[23]: u'FooBar'
cleaned = u"".join([line.strip() for line in u'\r\nFoo\r\nBar, FooBar'.split("\r\n")])
或只使用replace()
:
cleaned = u'\r\nFoo\r\nBar, FooBar'.replace("\r\n", "")
你可以做
'this is my string\r\nand here it continues'.replace('\r\n', '')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.