python如何制作英文字典

yipeiwu_com6年前Python基础

本文实例为大家分享了python制作英文字典的具体代码,供大家参考,具体内容如下

功能有添加单词,多次添加单词的意思,查询,退出,建立单词文件。

keys=[]
dic={}
def rdic():
  fr = open('dic.txt','r')  
  for line in fr:
    line = line.replace("\n",'')
    v = line.split(':')
    dic[v[0]] = v[1]
    keys.append(v[0])
  fr.close()
def centre():
  n = input("请输入进入相应模块(添加、查询、退出):")
  if n == "添加":
    key= input("plsease input English:")
    if key not in keys:
      value=input("please input Chinese:")
      dic[key]=value
      keys.append(key)
    else :
      t=input("如果添加新的意思请输入 Y,否则输入N:")
      if ( t=='Y'):
        temp=""
        temp=temp+dic[key]
        key1= input("请输入中文")
        temp=temp+","+key1
        print(temp)
        #keys.append(temp)
        dic[key]=temp
        print(dic)
        return 0
      else:
        return 0
  elif n== "查询":
    key= input("plsease input English:")
    print(keys)
    print(dic)
    if key not in keys:
      print("the english not in the dic.")
    else :
      print(dic[key])
  elif n == "退出" :
    return 1
  else :
    print("输入有误")
    return 0
def wdic():
  #print("!")
  with open('dic.txt','w') as fw:
    for k in keys:
      fw.write(k+':'+dic[k]+'\n')
def main():
  rdic()
  while True:
    print(keys)
    print(dic)
    n=centre()
    print(keys)
    print(dic)
    if n==1:
      break
    if n==0:
      continue
  wdic()
main()

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

python写程序统计词频的方法

python写程序统计词频的方法

在李笑来所著《时间当作朋友》中有这么一段: 可问题在于,当年我在少年宫学习计算机程序语言的时候,怎么可能想象得到,在20多年后的某一天,我需要先用软件调取语料库中的数据,然后用统计方法为...

在Python的setuptools框架下生成egg的教程

本文介绍了 setuptools 框架的内容,它是 PEAK 的一个副项目,它提供了比 distutils 更加简单的包管理和发行功能。 开始 setuptools 模块很会 “规避”。...

Pytorch 数据加载与数据预处理方式

Pytorch 数据加载与数据预处理方式

数据加载分为加载torchvision.datasets中的数据集以及加载自己使用的数据集两种情况。 torchvision.datasets中的数据集 torchvision.data...

python判断windows隐藏文件的方法

python判断windows隐藏文件的方法

1. 通过windows attrib 命令获取文件隐藏属性复制代码 代码如下:Syntax      ATTRIB [ + attri...

Python中sort和sorted函数代码解析

本文研究的主要是Python中sort和sorted函数的相关内容,具体如下。 一、sort函数 sort函数是序列的内部函数 函数原型: L.sort(cmp=None, key=No...