Advertisement
furas

Python - translator - dictionary

Jan 30th, 2017
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.32 KB | None | 0 0
  1. import cgi
  2. import cgitb
  3.  
  4. cgitb.enable()
  5.  
  6. print "Content-Type: text/html"
  7. print
  8.  
  9. form= cgi.FieldStorage()
  10.  
  11. try:
  12.     lang1 = form["startlanguage"].value
  13.     lang2 = form["finallanguage"].value
  14.    
  15.     word = form["word"].value
  16.    
  17.     if lang1 not in ("ita", "fra", "eng"):
  18.         print "Wrong start language:", lang1
  19.     elif lang2 not in ("ita", "fra", "eng"):
  20.         print "Wrong final language:", lang2
  21.     elif lang1 == lang2:
  22.         print "The same languages:", lang1, lang2
  23.     else:
  24.         found = False
  25.  
  26.         tranlation = {}
  27.  
  28.         ita_eng = open("itafraeng.txt", "r")
  29.        
  30.         for row in ita_eng:
  31.             word_ita, word_fra, word_eng = row.split()
  32.            
  33.             tranlation["ita"] = word_ita
  34.             tranlation["fra"] = word_fra
  35.             tranlation["eng"] = word_eng
  36.            
  37.             if word == tranlation[lang1]:
  38.                 found = True              
  39.                 print '''<html>
  40.                         <body>
  41.                         <p>%s = %s</p>
  42.                         </body>
  43.                         <html>''' % (word, tranlation[lang2])
  44.                 break # exit `for` loop
  45.        
  46.         ita_eng.close()        
  47.  
  48.         if not found:
  49.             print "Word not found"
  50.        
  51. except KeyError:
  52.    print ' Error '
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement