在邮件中,有以下行。这些行将被转换为 单行基于" List Num" 。对于打印,以下代码是 工作。但是当我尝试将变量存储为列表或字符串时,它无法正常工作。已经给出了以下代码。
mail = mailbody.splitlines()
nmail = ""
lnflg = "yes"
for line in mail:
if re.findall(r'List Num:\s*([A-Z][0-9]{5}[A-Z]*)', line) != []:
lnflg = "no"
print "\n"
if lnflg == "no":
print line.rstrip(os.linesep),
else:
print line
如果我使用for循环
打印,则下面的代码无法正常工作mail = mailbody.splitlines()
nmail = ""
lnflg = "yes"
for line in mail:
if re.findall(r'List Num:\s*([A-Z][0-9]{5}[A-Z]*)', line) != []:
lnflg = "no"
#print "\n"
nmail = nmail + "\n"
if lnflg == "no":
#print line.rstrip(os.linesep),
nmail = nmail + line.rstrip(os.linesep)
else:
nmail = nmail + line
for ln in nmail:
print ln
List Num: L08586 Proj/Prob: 09P0087 Application: ACI
Load to: TFDSA-SA Until: ON PROGRAM BASE Oncall Grp: TPFACI1
Requester: HZDSF1 - ABCD Work Phone: XX-XX-XXXXXXXX
Approver: FSDSJ8 Supervisor: ABCD XYZ
Updated: 04/28/2011 (01:05:46 E)
Segments: PESSFH PESGSDG
Purpose: THIS IS RELATED TO 09P0087
BARCODE WILL BE SUBMITTED IN ANOTHER PGMLST.
List Num: L08586 Proj/Prob: 09P0087 Application: ACI
Load to: TFDSA-SA Until: ON PROGRAM BASE Oncall Grp: TPFACI1
Requester: HZDSF1 - ABCD Work Phone: XX-XX-XXXXXXXX
Approver: FSDSJ8 Supervisor: ABCD XYZ
Updated: 04/28/2011 (01:05:46 E)
Segments: PESSFH PESGSDG
Purpose: THIS IS RELATED TO 09P0087
BARCODE WILL BE SUBMITTED IN ANOTHER PGMLST.
变量中的预期输出:
List Num: L08586 Proj/Prob: 09P0087 Application: ACI Load to: TFDSA-SA Until: ON PROGRAM BASE Oncall Grp: TPFACI1 Requester: HZDSF1 - ABCD Work Phone: XX-XX-XXXXXXXX Approver: FSDSJ8 Supervisor: ABCD XYZ Updated: 04/28/2011 (01:05:46 E) Segments: PESSFH PESGSDG Purpose: THIS IS RELATED TO 09P0087 BARCODE WILL BE SUBMITTED IN ANOTHER PGMLST.
List Num: L08586 Proj/Prob: 09P0087 Application: ACI Load to: TFDSA-SA Until: ON PROGRAM BASE Oncall Grp: TPFACI1 Requester: HZDSF1 - ABCD Work Phone: XX-XX-XXXXXXXX Approver: FSDSJ8 Supervisor: ABCD XYZ Updated: 04/28/2011 (01:05:46 E) Segments: PESSFH PESGSDG Purpose: THIS IS RELATED TO 09P0087 BARCODE WILL BE SUBMITTED IN ANOTHER PGMLST.
答案 0 :(得分:1)
您可以使用BARCODE
行来分隔groupby:
from itertools import groupby
with open("foo.txt") as f:
grouped, data = groupby(map(str.rstrip,f), key=lambda x: x.strip().startswith("BARCODE")), []
for k,v in grouped:
if not k:
data.append("".join(v)+"".join(next(grouped)[1]))
print(data[0])
print()
print(data[1])
输入:
List Num: L08586 Proj/Prob: 09P0087 Application: ACI
Load to: TFDSA-SA Until: ON PROGRAM BASE Oncall Grp: TPFACI1
Requester: HZDSF1 - ABCD Work Phone: XX-XX-XXXXXXXX
Approver: FSDSJ8 Supervisor: ABCD XYZ
Updated: 04/28/2011 (01:05:46 E)
Segments: PESSFH PESGSDG
Purpose: THIS IS RELATED TO 09P0087
BARCODE WILL BE SUBMITTED IN ANOTHER PGMLST.
List Num: L08586 Proj/Prob: 09P0087 Application: ACI
Load to: TFDSA-SA Until: ON PROGRAM BASE Oncall Grp: TPFACI1
Requester: HZDSF1 - ABCD Work Phone: XX-XX-XXXXXXXX
Approver: FSDSJ8 Supervisor: ABCD XYZ
Updated: 04/28/2011 (01:05:46 E)
Segments: PESSFH PESGSDG
Purpose: THIS IS RELATED TO 09P0087
BARCODE WILL BE SUBMITTED IN ANOTHER PGMLST.
输出:
List Num: L08586 Proj/Prob: 09P0087 Application: ACILoad to: TFDSA-SA Until: ON PROGRAM BASE Oncall Grp: TPFACI1Requester: HZDSF1 - ABCD Work Phone: XX-XX-XXXXXXXXApprover: FSDSJ8 Supervisor: ABCD XYZUpdated: 04/28/2011 (01:05:46 E)Segments: PESSFH PESGSDGPurpose: THIS IS RELATED TO 09P0087BARCODE WILL BE SUBMITTED IN ANOTHER PGMLST.
List Num: L08586 Proj/Prob: 09P0087 Application: ACILoad to: TFDSA-SA Until: ON PROGRAM BASE Oncall Grp: TPFACI1Requester: HZDSF1 - ABCD Work Phone: XX-XX-XXXXXXXXApprover: FSDSJ8 Supervisor: ABCD XYZUpdated: 04/28/2011 (01:05:46 E)Segments: PESSFH PESGSDGPurpose: THIS IS RELATED TO 09P0087BARCODE WILL BE SUBMITTED IN ANOTHER PGMLST.