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:
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?
CodePudding user response:
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