io - Iterating through a .txt file in Python and separating into strings -


i trying make python script iterates through .txt file. files 600-800 lines long , format of them follows:

========== id: 10001      found:(4) ========== msg: err_id  - ***error*** _errortexthere_  ========== id: 10002      found:(26) ========== msg: err_id  - ***error*** _errortexthere_ line2 line3 line4 line5  ========== id: 10003      found:(15039) ========== msg: err_id  - ***error*** _errortexthere_ etc1 etc2 etc3 

basically, want read 'id:' 'id:' , store of text between them in string (or array, dictionary, have you). problem is, amount of lines varies between 'id:'s managing them line number wouldn't help. new python , not familiar basic syntax other languages. have done fair amount of searching on , found many questions similar or close need, not precisely. appreciated.

here very simple implementation detects lines start exact string "id:". ignores blank lines , lines match ==========.

it saves lines following each id: dictionary, keys of dictionary being id strings.

from io import bytesio pprint import pprint  infile = bytesio(""" ========== id: 10001      found:(4) ========== msg: err_id  - ***error*** _errortexthere_  ========== id: 10002      found:(26) ========== msg: err_id  - ***error*** _errortexthere_ line2 line3 line4 line5 """)   buffer = "" d = {} id = none  line in infile:     if line.rstrip() in ("==========",""):         # skip blank lines or delimiting lines         pass     elif line.startswith("id: "):         # save buffer we've been collecting dictionary...         if id not none:                     d[id] = buffer          # ... , start collecting new lines         id = line.split()[1]         buffer = ""     else:         buffer += line else:     # save whatever lines leftover after last `id:`     if id not none:         d[id] = buffer  pprint(d) 

output:

{'10001': 'msg: err_id  - ***error*** _errortexthere_\n',  '10002': 'msg: err_id  - ***error*** _errortexthere_\nline2\nline3\nline4\nline5\n'} 

Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

jsf - How to ajax update an item in the footer of a PrimeFaces dataTable? -

django - CSRF verification failed. Request aborted. CSRF cookie not set -