执行以下行时:
df = df[df['Directory'].str.contains("C:\Windows\System32\Tasks")]
我收到以下错误:
File "/Users/patrickmaynard/Desktop/CSVparser/parse.py", line 80, in parseFoundFiles
df = df[df['Directory'].str.contains("C:\Windows\System32\Tasks")]
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pandas/core/strings.py", line 1562, in contains
regex=regex)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pandas/core/strings.py", line 249, in str_contains
regex = re.compile(pat, flags=flags)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/re.py", line 233, in compile
return _compile(pattern, flags)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/re.py", line 301, in _compile
p = sre_compile.compile(pattern, flags)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/sre_compile.py", line 562, in compile
p = sre_parse.parse(p, flags)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/sre_parse.py", line 856, in parse
p = _parse_sub(source, pattern, flags & SRE_FLAG_VERBOSE, False)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/sre_parse.py", line 415, in _parse_sub
itemsappend(_parse(source, state, verbose))
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/sre_parse.py", line 501, in _parse
code = _escape(source, this, state)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/sre_parse.py", line 401, in _escape
raise source.error("bad escape %s" % escape, len(escape))
sre_constants.error: bad escape \T at position 19
我尝试了其他文件路径,它们工作正常。我没有包含更多代码,因为我确定它不会影响这一行。我认为这可能是熊猫或正则表达式中的一个奇怪问题,是这种情况还是我犯了一个错误?
答案 0 :(得分:2)
str.contains
试图在默认情况下使用正则表达式,因此\T
试图被读取为特殊字符。您可以告诉它不要使用正则表达式,并通过说regex=False
来搜索确切的字符串:
df[df['Directory'].str.contains("C:\Windows\System32\Tasks", regex=False)]
示例:
>>> df
Directory
0 C:\Windows\System32\Tasks\123
1 C:\Windows\System32\Tasks\456
2 C:\Windows\
3 xyz
>>> df[df['Directory'].str.contains("C:\Windows\System32\Tasks", regex=False)]
Directory
0 C:\Windows\System32\Tasks\123
1 C:\Windows\System32\Tasks\456
答案 1 :(得分:0)
您可以按照解决方案posted here:
<块引用>您也可以尝试将 import re
替换为 import regex as re