Python RegEx 报告存在字符串的行
Python RegEx to report lines a string exists on
我正在尝试快速编写一些内容,以找到特定的字符串并向我报告这些字符串所在的行。我试图找到指数,所以我正在寻找 "e+" ,它只出现在以 AAA 开头的行上,但并非所有以 AAA 开头的行都包含 e+。目前,我的代码如下所示:
import re
file = raw_input("Enter a file to scan ")
extfile = file+".txt"
#NDoc = raw_input("Enter a file to place results ")
#log = open (NDoc, 'w')
xfile = open(file+".txt")
expcnt = 0
nl = list
for line in xfile:
line = line.strip()
n = re.findall('^PV1.+(e\+)')
if len(n) > 0:
expcnt = expcnt+1
### l = line n is on
nl.append(l)
for item in nl:
# log.write(item+"\n")
print (expcnt,"exponenets exist and occur on the following lines:"
For item in nl:
print item
#print ("Your results can be found in",extfile,".")
基本上,我想知道我需要在带有 3 个主题标签 (#) 的行中添加什么,以便记录找到第 n 行并将其放入列表中。稍后,我想打印该列表。顶部的 NDoc 和日志被注释掉了,因为我正在考虑使用 from __future__ import print_function
将结果打印到新页面,但现在可以忽略。所以我试图将所有行号放入列表 nl 中,将所有指数字符串放入列表 el 中(尽管 list el 只是让我仔细检查一下,以后不会用到)。
任何帮助将不胜感激,提前致谢!
编辑:
import re
file = raw_input("Enter a file to scan ")
extfile = file+".txt"
#NDoc = raw_input("Enter a file to place results ")
#log = open (NDoc, 'w')
xfile = open(file+".txt")
expcnt = 0
nl = list
for line_num, line in enumerate(xfile):
line = line.strip()
n = re.findall('^PV1.+(e\+)')
if len(n) > 0:
expcnt = expcnt+1
l = line_num
nl.append(l)
for item in nl:
# log.write(item+"\n")
#print (expcnt,"exponenets exist and occur on the following lines:"
for item in nl:, does anyone know why that would occur?
print item
#print ("Your results can be found in",extfile,".")
出于某种原因,这在 for itme in nl:
处返回错误 "Expected an indented block"
由于您已经在逐行阅读文件,因此您可以使用 enumerate
.
for line_num, line in enumerate(xfile):
而不是
for line in xfile:
我正在尝试快速编写一些内容,以找到特定的字符串并向我报告这些字符串所在的行。我试图找到指数,所以我正在寻找 "e+" ,它只出现在以 AAA 开头的行上,但并非所有以 AAA 开头的行都包含 e+。目前,我的代码如下所示:
import re
file = raw_input("Enter a file to scan ")
extfile = file+".txt"
#NDoc = raw_input("Enter a file to place results ")
#log = open (NDoc, 'w')
xfile = open(file+".txt")
expcnt = 0
nl = list
for line in xfile:
line = line.strip()
n = re.findall('^PV1.+(e\+)')
if len(n) > 0:
expcnt = expcnt+1
### l = line n is on
nl.append(l)
for item in nl:
# log.write(item+"\n")
print (expcnt,"exponenets exist and occur on the following lines:"
For item in nl:
print item
#print ("Your results can be found in",extfile,".")
基本上,我想知道我需要在带有 3 个主题标签 (#) 的行中添加什么,以便记录找到第 n 行并将其放入列表中。稍后,我想打印该列表。顶部的 NDoc 和日志被注释掉了,因为我正在考虑使用 from __future__ import print_function
将结果打印到新页面,但现在可以忽略。所以我试图将所有行号放入列表 nl 中,将所有指数字符串放入列表 el 中(尽管 list el 只是让我仔细检查一下,以后不会用到)。
任何帮助将不胜感激,提前致谢!
编辑:
import re
file = raw_input("Enter a file to scan ")
extfile = file+".txt"
#NDoc = raw_input("Enter a file to place results ")
#log = open (NDoc, 'w')
xfile = open(file+".txt")
expcnt = 0
nl = list
for line_num, line in enumerate(xfile):
line = line.strip()
n = re.findall('^PV1.+(e\+)')
if len(n) > 0:
expcnt = expcnt+1
l = line_num
nl.append(l)
for item in nl:
# log.write(item+"\n")
#print (expcnt,"exponenets exist and occur on the following lines:"
for item in nl:, does anyone know why that would occur?
print item
#print ("Your results can be found in",extfile,".")
出于某种原因,这在 for itme in nl:
由于您已经在逐行阅读文件,因此您可以使用 enumerate
.
for line_num, line in enumerate(xfile):
而不是
for line in xfile: