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

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -