程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

[Python practical basis] how to realize an English Chinese translation dictionary

編輯:Python

Catalog

One 、 Actual combat scene

Two 、 Main knowledge points

3、 ... and 、 Rookie actual combat

1、 establish python file

2、 File directory

3、 Running results


One 、 Actual combat scene

English Chinese Dictionary : Type in English , Return to corresponding Chinese .

Two 、 Main knowledge points

  • File read and write
  • Basic grammar
  • exception handling
  • Loop statement
  • string manipulation

3、 ... and 、 Rookie actual combat

Make arrangements now !

1、 establish python file

'''
Author: Rookie actual combat
Actual combat scene : How to realize an English Chinese translation dictionary
'''
# Import system package
import platform
print("Hello, Rookie actual combat ")
print(" Actual combat scene : How to realize an English Chinese translation dictionary ")
# English Chinese Dictionary
eng_hans_dict = {}
with open("py014.txt", encoding="utf8") as fin:
# Read the file , Save translation data
for line in fin:
if len(line.strip()) > 3:
eng, hans = line.strip().split(",")
eng_hans_dict[eng] = hans
print(" English Chinese dictionary data : %s -> %s" % (eng, hans))
def translate(input_word):
# translate
try:
return eng_hans_dict[input_word]
except KeyError:
return " Words are not in the dictionary "
input_word = input(" Please enter the English words to be translated : ").lower()
trans_result = translate(input_word)
print("%s The result of translation is : %s" % (input_word, trans_result))
print("Python edition ", platform.python_version())

2、 File directory

 py-014/
├── py014.txt
└── py014.py

3、 Running results

Hello, Rookie actual combat
Actual combat scene : How to realize an English Chinese translation dictionary  
English Chinese dictionary data : apple -> Apple
English Chinese dictionary data : banana -> Banana
English Chinese dictionary data : blueberry -> blueberries
English Chinese dictionary data : cherry -> Cherry
English Chinese dictionary data : crabapple -> Begonia fruit
English Chinese dictionary data : carambola -> Carambola
English Chinese dictionary data : chestnut -> chestnuts
English Chinese dictionary data : coconut -> Coconut
English Chinese dictionary data : cranberry -> Manyueberry
English Chinese dictionary data : cumquat -> Kumquat
English Chinese dictionary data : orange -> orange
English Chinese dictionary data : pear -> pear
English Chinese dictionary data : peach -> peach
English Chinese dictionary data : grape -> grapes
English Chinese dictionary data : lemon -> lemon
English Chinese dictionary data : lichee -> litchi
English Chinese dictionary data : loquat -> Loquat
English Chinese dictionary data : mango -> Mango.
Please enter the English words to be translated : apple
apple The result of translation is : Apple
Python edition 3.10.4

  Rookie actual combat , Continuous learning !


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved