0

I am getting a text like below by reading a word file

Exe Command\r\x07

My desired text is

Exe Command

I tried this solution but it gives me

 Exe Command\r

How can i remove 2 any backslash characters? I would like a speed friendly solution because I have thousands of inputs like this.

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
Anudocs
  • 686
  • 1
  • 13
  • 54

1 Answers1

0

You can use replace() method twice.

In [1]: myStr.replace("\r", "").replace("\x07", "")
Out[1]: 'Exe Command'

If this isn't working, you can try using raw string

In [1]: myStr.replace(r"\r", "").replace(r"\x07", "")
Out[1]: 'Exe Command'

EDIT: As per comment, for removing any of those control characters, use this post's solution.

import unicodedata
def remove_control_characters(s):
    return "".join(ch for ch in s if unicodedata.category(ch)[0]!="C")

All credits for this solution goes to Alex Quinn.

Alexander Santos
  • 1,458
  • 11
  • 22