how to read a specific line which starts in "#" from file in python -


how can read specific line starts in "#" file in python , set line key in dictionary (without "#") , set lines after line until next "#" value dictionary please me
here file :

01

please me :(

from collections import defaultdict  key = 'nokey' d = defaultdict(list)  open('thefile.txt', 'r') f:     line in f:         if line.startswith('#'):             key = line.replace('#', '')             continue         d[key].append(line) 

your dictionary have list of lines under each key. lines come before first line starting '#' stored under key 'nokey'.


Comments

Popular posts from this blog

c++ - llvm function pass ReplaceInstWithInst malloc -

java.lang.NoClassDefFoundError When Creating New Android Project -

Decoding a Python 2 `tempfile` with python-future -