[英]How to grab all lines after a match in Python
I am very new to Python and I saw a post here: How to grab the lines AFTER a matched line in python我对 Python 很陌生,我在这里看到了一篇文章: 如何在 python 中的匹配行之后抓取行
I have a file that has data in the following format: DEFINE JOBPARM ID=(ns_ppprtg_notify,nj_pprtd028_notify,0028)我有一个包含以下格式数据的文件:DEFINE JOBPARM ID=(ns_ppprtg_notify,nj_pprtd028_notify,0028)
SUBFILE='/sybods/prod/ca_scripts/ca_notify.ksh' SUBUSER=pbods SUBPASS=*PASSWORD*
I am attempting to extract each parameter so that I can construct the full command line.我试图提取每个参数,以便我可以构建完整的命令行。
Here is what I have so far:这是我到目前为止所拥有的:
with open(myExtract) as Extract:
for line in Extract:
currentLine = line.strip("\n").strip()
if currentLine.startswith("DEFINE JOBPARM ID=") or \
currentLine.contains("PARM")):
logger.info("Job Parameter line found")
if "DEFINE JOBPARM ID=" in currentLine:
tJobParmString = currentLine.partition("DEFINE JOBPARM ID=")[2].parition("SUBFILE")[0].strip()
tSubFileString = currentLine.partition("SUBFILE")[2].partition("SUBUSER")[0].strip()
tSubUserString = currentLine.partition("SUBUSER")[2].partition("SUBPASS")[0].strip()
if re.match(" PARM", currentLine, flags=0):
logger.info("PARM line found")
This is where I am stuck... I don't know for sure that there will be 2 PARM lines or 10. They all start with " PARM" and have numbers.这就是我被卡住的地方...我不确定会有 2 条 PARM 行还是 10 行。它们都以“PARM”开头并有数字。 How do I extract the values for PARM1, PARM2, PARM3 etc?
如何提取 PARM1、PARM2、PARM3 等的值? Each of the PARM lines (PARM1, PARM2) always starts on a new line.
每个 PARM 行(PARM1、PARM2)总是从一个新行开始。
Any pointers would be appreciated.任何指针将不胜感激。
Here you go mate.来了,伙计。 Here's my attempt at your problem.
这是我对您的问题的尝试。 I've given you three options in the data structure layout;
我在数据结构布局中给了你三个选项;
outDict
which maps the PARMs to the strings; outDict
将outDict
映射到字符串; outListOfTups
which gives you the same data as a list of tuples; outListOfTups
为您提供与元组列表相同的数据; and outList
which assumes you're not interested in the PARMS:和
outList
假设您对 PARMS 不感兴趣:
f = open("data.txt", 'r')
lines = f.read().splitlines()
outDict = {}
outListOfTups = []
outList = []
for line in lines:
if line.startswith('PARM'):
splitPoint = line.find("'")
name = line[:splitPoint-1]
string = line[splitPoint:].replace("'", "")
outDict[name] = string #if you want to work with dict
outListOfTups.append((name, string)) #if you want lists
outList.append(string)
print(outDict)
print(outList)
print(outListOfTups)
Let me know if I've misunderstood where you're coming from - this works for me on the input sample given.如果我误解了您的来源,请告诉我 - 这对给定的输入样本适用于我。
if("DEFINE JOBPARM ID=" in currentLine):
tJobParmString=currentLine.partition("DEFINE JOBPARM ID=")[2].partition("SUBFILE")[0].strip()
tSubFileString=currentLine.partition("SUBFILE")[2].partition("SUBUSER")[0].strip()
tSubUserString=currentLine.partition("SUBUSER")[2].partition("SUBPASS")[0].strip()
if(re.match("PARM",currentLine,flags=0)):
logger.info("PARM line found")
tParmString=currentLine.partition("=")[2].strip()
logger.info(tParmString)
if(not "PARM" in currentLine):
continue
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.