#convert_textfile_to_dic
from collections import defaultdict
dic = defaultdict(list)
def isfloat(num):
try:
float(num)
return True
except ValueError:
return False
#reading the file
with open('JsimEq.txt') as f:
lines = [line.rstrip() for line in f if line!='']
# with open('jsimeq_test.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",'')
line= line.replace("ln",'')
line= line.replace("**",'^')
#seperating LHS and RHS
ls=line.split("=")
key=ls[0].rstrip()
s=''
#print("Key ", ls[0])
#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 empty strings any constant present in the list
val= list(set([i for i in sl if not isfloat(i) and i!='' ]))
#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)
# count = 0
# for key,val in dic.items():
# count+=len(val)+1
# print(key," ",val)
# print("length ",count)
Add a code snippet to your website: www.paste.org