I wrote this python script for handling dictionary file (which is a text file) like this:
Word goes after the '#' and the meanings of that word goes under the word seperated by \n.
...
#hello
xxx
yyy
zzz
#another
ooo
I'want to add words to file with this python script. But I don't know how to do that. And most confusing part is if the file has that word then add meanings to it.
./main.py add 'hello' 'xxx;yyy;zzz;'
This is how I can add the word and it's meanings to the file. How could I do that. I wrote many scripts to accomplish and they're:
def add(d,w,m):
dic = dictPath+d
nwords = '\n'.join(m.split(";"))
wmean = '#'+w+'\n'+nwords
here = False
lnum=0
with open(dic,"r") as rf:
for line in rf:
if line == "#"+w+"\n": ## 'cause every line ends with \n
here="yes"
lnum+=1
else:
lnum+=1
if here == "no":
with open(dic,"a") as af:
af.writelines(wmean)
else:
with open(dic,"w") as wf:
data = rf.readlines()
data[lnum+1] = nwords
wf.writelines(data)
wf.close()
rf.close()
af.close()
and
def add(d,w,m):
dic = dictPath+d
nwords = '\n'.join(m.split(";"))
wmean = '#'+w+'\n'+nwords
with open(dic,"r+") as f:
for line in f:
if line == "#"+w+"\n":
f.write(wmean)