【问题标题】:Python remove line breaks [duplicate]Python删除换行符[重复]
【发布时间】:2016-02-09 02:30:25
【问题描述】:

我正在从这样的程序中读取输出

test_output = test_run.communicate(input=str.encode(input))[0]
output = test_output .decode('utf-8').strip()

如果程序像这样返回新行

>>> a
>>> b

它是这样读的(带有repr()

>>> 'a\r\nb'

我尝试用

删除换行符
output.replace(r"\r\n","")

但它不起作用,如果我像普通字符串一样打印它会返回

>>> a
>>> b

和repr()

>>> 'a\r\nb'

如何从我的字符串中删除 \r\n?

【问题讨论】:

标签: python


【解决方案1】:

不要使用正则表达式,正则表达式不会做你认为它做的事情:

>>> 'a\r\nb'.replace('\r\n', '')
'ab'

即只需删除replace(r'\r\n', '')中的r

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多