Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I avoid functions to read escape characters?

I have a list string that depicts directory paths, e.g.:

| 'directory path' |
|:----------------:|
|'c:\\windows\\g\\subfolder1'|
|'c:\\windows\\g\\subfolder2'|
|'etc' |

The string is perfectly valid and when I print them they come out naturally as:

print(dir_list[0])
dir_list[0]

c:\windows\g\subfolder
Out[1]: 'c:\\windows\\g\\subfolder1'

However, when I use the string in a function I get the following error:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

df['directory path'].str.contains(dir_list[0])
error: bad escape \g at position 10

Why do I get the error in the function when it works totally fine as a string?

>Solution :

You need to use regex=False as str.contains considers the pattern a regex by default:

df['directory path'].str.contains(dir_list[0], regex=False)

Or, if for some reason you need to keep the default regex=True, escape the characters:

import re
df['directory path'].str.contains(re.escape(dir_list[0]))

Output:

0     True
1    False
2    False
Name: directory path, dtype: bool
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading