from collections import defaultdict
dic = defaultdict(list)
#reading the lines
with open('jsimeq.txt') as f:
lines = [line.rstrip() for line in f if line!='']
#print(lines)
for line in lines:
#removing blank lines
if line!='':
line=line.replace("exp",'')
#seperating LHS and RHS
ls=line.split("=")
key=ls[0]
s=''
#Processing the RHS
for i in ls[1]:
#Replace all Arithematical Symbols with '@'
if i in "+-/*":
s+='@'
continue
# remove the Parenthesis
if i not in " ([])":
s+=i
#print(s)
#sperate all the variables in RHS basis '@'
sl=s.split("@")
#print(sl)
#Removing any constant present in the list
val= list(set([i for i in sl if i not in "0123456789"]))
#print(val)
# check if the given LHS is already present in the dictionary
if len(dic[key])>0 and dic[key][-1]=='':
dic[key][-1]=val
else:
for i in val:
dic[key].append(i)
print(dic)
for key,val in dic.items():
print(key," ",val)
Add a code snippet to your website: www.paste.org