Python 如何求解sre_常数。错误:对于特殊情况*CLK,在位置0处无需重复任何操作*
我有一个熊猫数据框Python 如何求解sre_常数。错误:对于特殊情况*CLK,在位置0处无需重复任何操作*,python,pandas,Python,Pandas,我有一个熊猫数据框 import pandas as pd df = pd.read_csv("MyCsv.csv", delimiter='@@@') df ID Signal 0 0 HT_CLKIN_P 1 1 HT_CLKOUT_P 2 2 LDTPHY013_Inst1.HT_REFCLK 3 3 clk_PCI1CLK_H 4 4 clk_ht100_or_200_H 5 5 clk_pcibr66_H 6 6 h_e
import pandas as pd
df = pd.read_csv("MyCsv.csv", delimiter='@@@')
df
ID Signal
0 0 HT_CLKIN_P
1 1 HT_CLKOUT_P
2 2 LDTPHY013_Inst1.HT_REFCLK
3 3 clk_PCI1CLK_H
4 4 clk_ht100_or_200_H
5 5 clk_pcibr66_H
6 6 h_extrxclkin
7 7 h_exttxclkin
def filterData(df,colname,regex):
'''
df:: Dataframe Name
colname: Name of the column against which you want to filter the data.
regex: Regular expression or special characters you want to search.
'''
return df[df[colname].str.contains(regex,regex=True)]
filterData(df,'Signal','clk_ht100*')
filterData(df,'Signal','*CLK*')
我得到以下错误
---------------------------------------------------------------------------
error Traceback (most recent call
last)
<ipython-input-9-32fc02914557> in <module>()
----> 1 filterData(df,'Signal','*CLK*')
<ipython-input-8-aeebba3ee8c6> in filterData(df, colname, regex)
5 regex: Regular expression or special characters you want to search.
6 '''
----> 7 return df[df[colname].str.contains(regex,regex=True)]
~\AppData\Local\Continuum\anaconda3\lib\site-
packages\pandas\core\strings.py in contains(self, pat, case, flags, na, regex)
1565 def contains(self, pat, case=True, flags=0, na=np.nan, regex=True):
1566 result = str_contains(self._data, pat, case=case, flags=flags, na=na,
-> 1567 regex=regex)
1568 return self._wrap_result(result)
1569
~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\strings.py in str_contains(arr, pat, case, flags, na, regex)
247 flags |= re.IGNORECASE
248
--> 249 regex = re.compile(pat, flags=flags)
250
251 if regex.groups > 0:
~\AppData\Local\Continuum\anaconda3\lib\re.py in compile(pattern, flags)
231 def compile(pattern, flags=0):
232 "Compile a regular expression pattern, returning a pattern object."
--> 233 return _compile(pattern, flags)
234
235 def purge():
~\AppData\Local\Continuum\anaconda3\lib\re.py in _compile(pattern, flags)
299 if not sre_compile.isstring(pattern):
300 raise TypeError("first argument must be string or compiled pattern")
--> 301 p = sre_compile.compile(pattern, flags)
302 if not (flags & DEBUG):
303 if len(_cache) >= _MAXCACHE:
~\AppData\Local\Continuum\anaconda3\lib\sre_compile.py in compile(p, flags)
560 if isstring(p):
561 pattern = p
--> 562 p = sre_parse.parse(p, flags)
563 else:
564 pattern = None
~\AppData\Local\Continuum\anaconda3\lib\sre_parse.py in parse(str, flags, pattern)
853
854 try:
--> 855 p = _parse_sub(source, pattern, flags & SRE_FLAG_VERBOSE, 0)
856 except Verbose:
857 # the VERBOSE flag was switched on inside the pattern. to be
~\AppData\Local\Continuum\anaconda3\lib\sre_parse.py in _parse_sub(source, state, verbose, nested)
414 while True:
415 itemsappend(_parse(source, state, verbose, nested + 1,
--> 416 not nested and not items))
417 if not sourcematch("|"):
418 break
~\AppData\Local\Continuum\anaconda3\lib\sre_parse.py in _parse(source, state, verbose, nested, first)
614 if not item or (_len(item) == 1 and item[0][0] is AT):
615 raise source.error("nothing to repeat",
--> 616 source.tell() - here + len(this))
617 if item[0][0] in _REPEATCODES:
618 raise source.error("multiple repeat",
error: nothing to repeat at position 0
---------------------------------------------------------------------------
错误回溯(最近的调用)
最后)
在()
---->1个过滤器数据(df、“信号”和“*CLK*”)
在filterData中(df、colname、regex)
5正则表达式:要搜索的正则表达式或特殊字符。
6 '''
---->7返回df[df[colname].str.contains(regex,regex=True)]
~\AppData\Local\Continuum\anaconda3\lib\site-
packages\pandas\core\strings.py包含(self、pat、case、flags、na、regex)
1565 def包含(self、pat、case=True、flags=0、na=np.nan、regex=True):
1566 result=str\u包含(self.\u数据,pat,case=case,flags=flags,na=na,
->1567正则表达式=正则表达式)
1568返回自包装结果(结果)
1569
str_中的~\AppData\Local\Continuum\anaconda3\lib\site packages\pandas\core\strings.py包含(arr、pat、case、flags、na、regex)
247标志|=重新标记
248
-->249 regex=re.compile(pat,flags=flags)
250
251如果regex.groups>0:
编译中的~\AppData\Local\Continuum\anaconda3\lib\re.py(模式、标志)
231 def编译(模式,标志=0):
232“编译正则表达式模式,返回模式对象。”
-->233返回编译(模式、标志)
234
235 def清除():
编译中的~\AppData\Local\Continuum\anaconda3\lib\re.py(模式、标志)
299如果不是sre_compile.isstring(模式):
300 raise TypeError(“第一个参数必须是字符串或编译模式”)
-->301 p=sre_compile.compile(模式、标志)
302如果不是(标志和调试):
303如果len(\u cache)>=\u MAXCACHE:
编译中的~\AppData\Local\Continuum\anaconda3\lib\sre\u compile.py(p,标志)
560如果是字符串(p):
561模式=p
-->562 p=sre_parse.parse(p,标志)
563其他:
564模式=无
解析中的~\AppData\Local\Continuum\anaconda3\lib\sre\u parse.py(str、标志、模式)
853
854尝试:
-->855 p=_parse_sub(源、模式、标志和SRE_标志详细,0)
856除冗长外:
857#模式内的详细标志已打开。成为
sub中的~\AppData\Local\Continuum\anaconda3\lib\sre\u parse.py(源、状态、详细、嵌套)
414虽然正确:
415项结束(_解析(源、状态、详细、嵌套+1、,
-->416非嵌套和非项目)
417如果不是sourcematch(“|”):
418中断
~\AppData\Local\Continuum\anaconda3\lib\sre\u parse.py in\u parse(源、状态、详细、嵌套、第一)
614如果不是项目或(_len(项目)==1且项目[0][0]位于):
615 raise source.错误(“无需重复”,
-->616 source.tell()-here+len(this))
617如果重复代码中的[0][0]项:
618引发源错误(“多次重复”,
错误:在位置0没有要重复的内容
在正则表达式中,*
是一个量词,表示前面的模式应该匹配零次或多次。由于正则表达式的格式不正确,因此在位置0处不会抛出任何要重复的错误。量词不能应用于任何模式,因为没有可应用的模式。我建议你阅读了正则表达式的一些基础知识。Python re模块有文档记录
你的意思可能是这样的表达:
filterData(df,'Signal','clk_ht100.*')
filterData(df,'Signal','.*CLK.*')
>>> df['Signal'].str.startswith("clk_ht100")
0 False
1 False
2 False
3 False
4 True
5 False
6 False
7 False
Name: Signal, dtype: bool
>>> df['Signal'].str.contains("CLK")
0 True
1 True
2 True
3 True
4 False
5 False
6 False
7 False
Name: Signal, dtype: bool
字符串匹配也可以实现同样的效果,不需要正则表达式:
filterData(df,'Signal','clk_ht100.*')
filterData(df,'Signal','.*CLK.*')
>>> df['Signal'].str.startswith("clk_ht100")
0 False
1 False
2 False
3 False
4 True
5 False
6 False
7 False
Name: Signal, dtype: bool
>>> df['Signal'].str.contains("CLK")
0 True
1 True
2 True
3 True
4 False
5 False
6 False
7 False
Name: Signal, dtype: bool
您可以在文档底部找到可用字符串方法的列表。您能给我指一下文档中我们可以支持哪些正则表达式吗pandas@TechiTechi我在标准文档中添加了一些链接。这没什么特别的,你也可以通过谷歌很快找到。