Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy does my python dictionary not updating properly?
    text
    copied!<p>Hello I am trying to store my words I translate using the google api so I don't have to do the same translation twice. So here is my code:</p> <pre><code>def loadtransdict(fro, to): try: tdictf = open("transdict"+fro+"-"+to+".txt", "r") pairs = tdictf.read().split(",") tdictf.close() tdict = {} for pair in pairs: tdict[pair.split(":")[0]] = pair.split(":")[1] return tdict except: tdictf = open("transdict"+fro+"-"+to+".txt", "w") tdictf.close() return {} else: return None def writetodict(fro, to, sword, dword): try: tdictf = open("transdict"+fro+"-"+to+".txt", "r") if tdictf.read() == "": tdictf = open("transdict"+fro+"-"+to+".txt", "a") tdictf.write(sword+":"+dword) else: tdictf = open("transdict"+fro+"-"+to+".txt", "a") tdictf.write(","+sword+":"+dword) tdictf.close() except: return None def translate(original_word, fro, to): tdict = loadtransdict(fro, to) if original_word in tdict: return tdict[original_word] else: print original_word print tdict #mm = requests.get('http://api.mymemory.translated.net/get?q='+word+'&amp;langpair='+fro+'|'+to) gt = requests.get('https://www.googleapis.com/language/translate/v2?key=MYKEY='\ +original_word+'&amp;source='+fro+'&amp;target='+to+'&amp;prettyprint=false') translated_word = re.search("translatedText\":\"(.*)\"", gt.text).group(1) writetodict(fro,to,original_word,translated_word) return translated_word </code></pre> <p>where transdicten-es.txt is a file containing translations written to it in the following format: </p> <blockquote> <p>constantly:constantemente,commanded:ordenado,damp:humedad,mistaken:equivocado,dignity:dignidad</p> </blockquote> <p>My trouble is that often words that have already been translated end up being translated again, not just retrieved from the dictionary and I cannot work out why! If it helps, translate() is being called many 1000s of times in a row in a for loop. Thanks.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload