Experts,
I have written a program to convert the string into dictionary. I'm able to achieve the desired result but i doubt if this is a pythonic way. Would like to hear suggestions on the same.
txt = '''
name : xxxx
desgination : yyyy
cities :
LA : Los Angeles
NY : New York
HeadQuarters :
LA : LA
NY : NY
Country : USA
'''
I have split using (:) and have stored in dictionary. Here Cities and HeadQuarters contains another dictionary for which i have written code like this.
if k == 'cities' :
D[k] = {}
continue
elif k == 'HeadQuarters':
D[k] = {}
continue
elif k == 'LA' :
if D.has_key('cities'):
if D['cities'].get(k) is None:
D['cities'][k] = v
if D.has_key('HeadQuarters'):
if D['HeadQuarters'].get(k) is None:
D['HeadQuarters'][k] = v
elif k == 'NY' :
if D.has_key('cities'):
if D['cities'].get(k) is None:
D['cities'][k] = v
if D.has_key('HeadQuarters'):
if D['HeadQuarters'].get(k) is None:
D['HeadQuarters'][k] = v
else:
D[k]= v