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

后端 未结 6 1206
鱼传尺愫
鱼传尺愫 2021-02-20 11:36

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 12:03

    If you want to normalise whitespace (replace runs of one or more whitespace characters by a single space, and strip leading and trailing whitespace) this can be accomplished by using string methods:

    >>> text = '   foo\tbar\r\nFred  Nurke\t Joe Smith\n\n'
    >>> ' '.join(text.split())
    'foo bar Fred Nurke Joe Smith'
    

提交回复
热议问题