Regex 使用正则表达式提取数据,并在Python2.7中对其进行计数
我需要使用正则表达式从文本中提取“Author”一词,并对其进行计数。这是我代码的一部分:Regex 使用正则表达式提取数据,并在Python2.7中对其进行计数,regex,python-2.7,count,expression,Regex,Python 2.7,Count,Expression,我需要使用正则表达式从文本中提取“Author”一词,并对其进行计数。这是我代码的一部分: counts=0 for line in handle: line=line.rstrip() counts=counts+1 x=re.findall('(^Author)', line) if len(myword)> 0: print counts,x print counts, "lines that matches Author" 我可以打开文件,
counts=0
for line in handle:
line=line.rstrip()
counts=counts+1
x=re.findall('(^Author)', line)
if len(myword)> 0: print counts,x
print counts, "lines that matches Author"
我可以打开文件,找到单词,毫无问题地得到职位,但我不知道如何计算它们。有人能帮帮我吗?
谢谢。为什么是正则表达式?因为我想学习如何掌握正则表达式。请使用另一个变量
totals=0
(靠近count=0
行)。然后,在循环中放入total+=len(x)
Why regex?因为我想学习如何掌握正则表达式。请使用另一个变量totals=0
(靠近count=0
行)。然后,在循环中放入total+=len(x)