我正在尝试格式化下面字符串变量中的数据以打印“Patchset lock”之后的最新评论,我在下面给出了预期输出,有人可以建议如何修复它吗?
string = '''Patch Set 1:
This change is being verified in bugsfor the following manifests along with other changes as detailed below:
=====================================================================================
git-android.comp.com/platform/manifest:kk:default.xml
=====================================================================================
https://commander.company.com/commander/pages/SimplifiedJobView/LoadComponent_run?jobId=3217513
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------
o https://review-android.comp.com/#change,
Please note that verification of all changes in this batch need to be successful before this change can be merged.
PLEASE DO NOT UPLOAD A NEW PATCH SET, OR REMOVE APPROVALS UNTIL THE VERIFICATION IS COMPLETE.
Patch Set 1: Verified
Successful bugsverification. You may find the results of the verification by following the link(s) below:
https://commander.company.com/commander/pages/SimplifiedJobView/LoadComponent_run?jobId=3217513
Patch Set 2: Failed
Failed bugsverification. You may find the results of the verification by following the link(s) below:
https://commander.company.com/commander/pages/SimplifiedJobView/LoadComponent_run?jobId=3217523'''
'''
预期输出: -
Failed bugsverification. You may find the results of the verification by following the link(s) below:
https://commander.company.com/commander/pages/SimplifiedJobView/LoadComponent_run?jobId=3217523"
'''
答案 0 :(得分:1)
不确定你的意思(顺便说一下你的第一个代码有IndentationError
),但这可能会起作用:
>>> print(string.rsplit('Patch Set ', 1)[1].split('\n', 1)[1])
Failed bugsverification. You may find the results of the verification by following the link(s) below:
https://commander.company.com/commander/pages/SimplifiedJobView/LoadComponent_run?jobId=3217523
说明:.rsplit('Patch Set ', 1)[1]
在Patch Set
(rsplit
)的最后一次出现时分割,得到第二部分([1]
),.split('\n', 1)[1]
分裂第一次出现\n
(split
)并得到第二个标准杆([1]
)
答案 1 :(得分:0)
它有点棘手。
Basicly:
"Patch Set "
\n
; 不知道Python中的函数,但基本上应该这样做。