Python dictionary editing entries -


def replace_acronym(): # function not yet implemented     #find     abbr, text in acronyms.items():         if abbr == acronym_edit.get():             textadd.insert(0,text)      #delete     name = acronym_edit.get().upper()     name.upper()     r =dict(acronyms)     del r[name]     open('acronym_dict.py','w')as outfile:         outfile.write(str(r))         outfile.close() # uneccessary explicit closure since used with...     message ='{0} {1} {2} \n '.format('removed', name,'with text database.')     display.insert('0.0',message)      #add     abbr_in = acronym_edit.get()     text_in = add_expansion.get()     acronyms[abbr_in] = text_in     # write amended dictionary     open('acronym_dict.py','w')as outfile:         outfile.write(str(acronyms))         outfile.close()     message ='{0} {1}:{2}{3}\n  '.format('modified entry', abbr_in,text_in, 'added')     display.insert('0.0',message) 

i trying add functionality of editing dictionary entries in tkinter widget. dictionary in format {acronym: text, acronym2: text2...}

what thought function achieve find entry in dictionary, delete both acronym , associated text , add whatever acronym , text have been changed to. happens example if have entry test: test , want modify text: abc returned function text: testabc - appending changed text although have (i thought) overwritten file.

what doing wrong?

that's pretty messy lookin' function. acronym replacement can done pretty simple:

acronyms = {'sonar': 'sound navigation , ranging',             'html': 'hypertext markup language',             'css': 'cascading style sheets',             'test': 'test',             'scuba': 'self contained underwater breathing apparatus',             'radar': 'radio detection , ranging',            }  def replace_acronym(a_dict,check_for,replacement_key,replacement_text):     c = a_dict.get(check_for)     if c not none:         del a_dict[check_for]         a_dict[replacement_key] = replacement_text     return a_dict  new_acronyms = replace_acronym(acronyms,'test','text','abc') 

that works perfect me (in python 3). call in function writes new_acronyms dict file or whatever else want 'cause it's no longer tied being written file.


Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -