无法基于python中的字符串获取特定行

时间:2019-03-19 09:29:14

标签: python python-2.7 python-regex

file1.txt:

" > git rev-parse refs/remotes/origin/master^{commit} # timeout=10",
            " > git rev-parse refs/remotes/origin/origin/master^{commit} # timeout=10",
            "Checking out Revision ff1b75913a79ffe7fc066bc962f74afa5e6f563a (refs/remotes/origin/master)",
            " > git config core.sparsecheckout # timeout=10",
          " > git checkout -f ff1b75913a79ffe7fc066bc962f74afa5e6f563a",
            " > git branch -a -v --no-abbrev # timeout=10",
            " > git checkout -b master ff1b75913a79ffe7fc066bc962f74afa5e6f563a",
            """Commit message: "Added parameter for jmeter for Jenkins"""",
            "[Pipeline] sh",
            "[petclinic] Running shell script",

我正在将文件内容读取到变量message中,并希望获取上述行:

Commit message: "Added parameter for jmeter for Jenkins

我正在使用python 2.7。请提出建议。我尝试使用:

indexofGitMsgList = [gin for gin, item in enumerate(message) if re.search('^Commit Message', item)]
indexofGitMsg = max(indexofGitMsgList)
gitMsgList = message[indexofGitMsg]
print(gitMsgList)

4 个答案:

答案 0 :(得分:0)

使用正则表达式-> Lookbehind & Lookahead

例如:

import re

message = '''" > git rev-parse refs/remotes/origin/master^{commit} # timeout=10",
            " > git rev-parse refs/remotes/origin/origin/master^{commit} # timeout=10",
            "Checking out Revision ff1b75913a79ffe7fc066bc962f74afa5e6f563a (refs/remotes/origin/master)",
            " > git config core.sparsecheckout # timeout=10",
          " > git checkout -f ff1b75913a79ffe7fc066bc962f74afa5e6f563a",
            " > git branch -a -v --no-abbrev # timeout=10",
            " > git checkout -b master ff1b75913a79ffe7fc066bc962f74afa5e6f563a",
            """Commit message: "Added parameter for jmeter for Jenkins"""",
            "[Pipeline] sh",
            "[petclinic] Running shell script",'''

m = re.search("(?<=Commit message:)(.*?)(?=,)", message)
if m:
    print(m.group(1).strip().strip('"'))

输出:

Added parameter for jmeter for Jenkins

答案 1 :(得分:0)

使用in搜索str,使用replace()摆脱"

list.txt:

" > git rev-parse refs/remotes/origin/master^{commit} # timeout=10",
" > git rev-parse refs/remotes/origin/origin/master^{commit} # timeout=10",
"Checking out Revision ff1b75913a79ffe7fc066bc962f74afa5e6f563a (refs/remotes/origin/master)",
" > git config core.sparsecheckout # timeout=10",
" > git checkout -f ff1b75913a79ffe7fc066bc962f74afa5e6f563a",
" > git branch -a -v --no-abbrev # timeout=10",
" > git checkout -b master ff1b75913a79ffe7fc066bc962f74afa5e6f563a",
"""Commit message: "Added parameter for jmeter for Jenkins"""",
"[Pipeline] sh",
"[petclinic] Running shell script",

因此

logFile = "list.txt"
with open(logFile) as f:
    content = f.readlines()
# you may also want to remove empty lines
content = [l.strip() for l in content if l.strip()]

searchStr = 'Commit message'

for line in content:
    if searchStr in line:
        print(line.replace('"', '')[:-1])

输出

Commit message: Added parameter for jmeter for Jenkins

编辑

要将消息存储在变量中:

searchStr = 'Commit message'
commitMsg = ''
for line in content:
    if searchStr in line:
        print(line.replace('"', '')[:-1])
        commitMsg = line.replace('"', '').split(": ")[1][:-1]

print(commitMsg)   # Added parameter for jmeter for Jenkins

答案 2 :(得分:0)

一个非常简单的示例是检查该行是否包含三个双引号(“”“)并将其保存在列表中。

with open('test.txt', 'r') as f:
    message = []
    for line in f.readlines():
    if '"""' in line:
        message.append(line)
    print(message)

答案 3 :(得分:0)

最简单的方法是使用python的findall正则表达式方法:

import re
message = '''" > git rev-parse refs/remotes/origin/master^{commit} # 
    timeout=10",
        " > git rev-parse refs/remotes/origin/origin/master^{commit} # 
    timeout=10",
        "Checking out Revision ff1b75913a79ffe7fc066bc962f74afa5e6f563a 
    (refs/remotes/origin/master)",
        " > git config core.sparsecheckout # timeout=10",
      " > git checkout -f ff1b75913a79ffe7fc066bc962f74afa5e6f563a",
        " > git branch -a -v --no-abbrev # timeout=10",
        " > git checkout -b master 
    ff1b75913a79ffe7fc066bc962f74afa5e6f563a",
        """Commit message: "Added parameter for jmeter for Jenkins"""",
        "[Pipeline] sh",
        "[petclinic] Running shell script",'''


m=(re.findall("Commit message:\s.+[a-zA-Z]+",message))
for i in m:
   print(i)

输出

Commit message: "Added parameter for jmeter for Jenkins