Bernardo Cavalcanti
Bernardo Cavalcanti

Reputation: 1

Trouble to search and find the special " \ " character inside a string?

I have a list of people names (string elements) and some of them are corrupted with a backward-slash '\' sitting somewhere in the middle of their names.

I would like to filter them outside, but am finding hard-to-explain issues...

To look for the \ backslash, I used .find method or in, they both seem to work identically.

The only parameter that seems to work is "\\", because "\" or r'\' will both result in error (escaping the final quote --> SyntaxError: EOL while scanning string literal)

Therefore, this is the search code:

names = ["robert", "rob\xadt", "egi\xadio"] for name in names: print(name.find('\\'))

This will return -1, -1, -1 (nothing found)

I cannot change the input string containing these names, however, for the sake of this issue, if I manually take one of these names and add the r raw before, then the code works, as in the last element below:

names = ["egi\xadio", r"egi\xadio"] for name in names: print(name.find('\\'))

This will return -1, 3 (found something!)

I tried adding the raw symbol r systematically, but did not work, as below:

name = 'rob\xadt' name = fr'{name}' name.find('\\')

Finally, both 'similar' codes below will not find the \, but I wonder why the first one results in error:

name = "rob\xar" name.find('\\')

SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 3-5: truncated \xXX escape

name = "rob\xad" name.find('\\')

ok

Any insights?? Thanks in advance!

Upvotes: 0

Views: 35

Answers (1)

Chanran Kim
Chanran Kim

Reputation: 449

name = r"rob\xar"
name.find('\\')

I think that this is what you want. Here, 'r' means 'raw' string.

String literals may optionally be prefixed with a letter 'r' or 'R'; such strings are called raw strings and use different rules for interpreting backslash escape sequences.

ref: https://docs.python.org/2/reference/lexical_analysis.html#string-literals

Upvotes: 1

Related Questions