python - 如何解决 sre_constants.error:对于特殊情况 *CLK* 在位置 0 无重复内容
问题描述
我有一个熊猫数据框
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
解决方案
在正则表达式中,*
是一个量词,表示前面的模式应该匹配零次或多次。抛出错误nothing to repeat at position 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
您可以在使用文本数据文档的底部找到可用字符串方法的列表。
推荐阅读
- clickhouse - 如何显示 ClickHouse 数据库中的表正在使用什么引擎?
- c# - Visual Studio 设计器不断弄乱控制
- angular - 如何在 Angular 表单中实现验证器
- stanford-nlp - 如何设置 stanford CRF 分类器来监听 http 请求?
- azure - Azure:无法从另一个触发发布管道
- node.js - 我想使用 nodejs 从 mongodb 获取记录
- javascript - 如何使用 vuejs 显示谷歌地图
- python - 如何在python中将文本文件转换为Excel文件
- devops - 为什么在使用 AWSJavaScriptSDK 时忽略 ElasticBeanstalk .ebextensions?
- javascript - Multisort array of objects in a specific way