删除 来自 python 字符串
当您在 Python 中通过 popen
运行某些内容时,结果来自缓冲区,并在每行末尾带有回车符 (13) 的 CR-LF 十进制值。如何从 Python 字符串中删除它?
When you run something through popen
in Python, the results come in from the buffer with the CR-LF decimal value of a carriage return (13) at the end of each line. How do you remove this from a Python string?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您只需将
所有出现的 CRNL 替换为 NL,这似乎就是您想要的。
You can simply do
to replace all occurrences of CRNL with just NL, which seems to be what you want.
如果它们位于字符串的末尾,我建议使用:
您也可以使用不带参数的 rstrip() ,这也会删除空格。
If they are at the end of the string(s), I would suggest to use:
You can also use rstrip() without parameters which will remove whitespace as well.
Replace('\r\n','\n') 应该可以工作,但有时却不能。多么奇怪啊。相反,你可以使用这个:
replace('\r\n','\n') should work, but sometimes it just does not. How strange. Instead you can use this:
实际上,您可以简单地执行以下操作:
这将删除字符串前导或尾随的所有无关空白,包括 CR 和 LF。
执行相同的操作,但仅尾随字符串。
即:
s now contains 'Now is the time for all good...'
s now contains 'Now is the time for all good...'
请参阅 http://docs.python.org/library/stdtypes.html 了解更多信息。
Actually, you can simply do the following:
This will remove any extraneous whitespace, including CR and LFs, leading or trailing the string.
Does the same, but only trailing the string.
That is:
s now contains 'Now is the time for all good...'
s now contains ' Now is the time for all good...'
See http://docs.python.org/library/stdtypes.html for more.
您也可以执行
s = s.replace('\r', '')
。You can do
s = s.replace('\r', '')
too.