Convert multiple lines to single line and store in a variable in python -


in mail, below lines there. these lines converted single line based on "list num" . printing, below code working. store in variable further analysis.

mail = mailbody.splitlines() nmail = "" lnflg = "yes" 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 

below code not working, if print using loop

mail = mailbody.splitlines() nmail = "" lnflg = "yes" 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 loop print nmail

    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: related 09p0087

        barcode submitted in 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: related 09p0087

        barcode submitted in pgmlst. 

expected output in variable:

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: related 09p0087 barcode submitted in 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: related 09p0087 barcode submitted in pgmlst.

it looks mistake here have comma(,) @ end of line.

nmail = nmail + line.rstrip(os.linesep), 

if have tuple type, can add item if tuple. start off string, means can add similar types it.

here's sample tuples shows problem:

>>> = (1,) >>> a+2 traceback (most recent call last):   file "<stdin>", line 1, in <module> typeerror: can concatenate tuple (not "int") tuple 

Comments

Popular posts from this blog

javascript - oscilloscope of speaker input stops rendering after a few seconds -

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -