Deleting specific control characters(\n \r \t) from a string

后端 未结 6 2214
再見小時候
再見小時候 2021-02-20 11:34

I have quite large amount of text which include control charachters like \\n \\t and \\r. I need to replace them with a simple space--> \" \". What is the fastest way to do this

6条回答
  •  名媛妹妹
    2021-02-20 11:49

    using regex

    re.sub(r'\s+', ' ', '1\n2\r3\t4')
    

    without regex

    >>> ' '.join('1\n\n2\r3\t4'.split())
    '1 2 3 4'
    >>>
    

提交回复
热议问题