Python 英语单词本
python pymysql re requests socket库的简单运用
要考试了,这里用所学的知识做一个实例
pymysql库
这个库是用来连接数据库的,使用数据库语句在python里创建表和数据库
English.py
import pymysql
def init(): #创建数据库和表
sql_data='create database English'
sql_table='create table word(id int(100),English char(50),Chinese varchar(100),time varchar(50))default charset=utf8;'
DB=pymysql.connect(host='localhost',password='1234',charset='utf8',user='root')
cursor=DB.cursor()
cursor.execute(sql_data)
cursor.execute('use English')
cursor.execute(sql_table)
init()
requests库
通过python来获取网页信息
function.py这个文件来实现单词本的各种功能
function.py
import time
import pymysql
import requests
DB = pymysql.connect(host='localhost', password='1234', charset='utf8', user='root', database='English')
cursor = DB.cursor()
class Func():
def __init__(self):
self.sql_all = 'select * from word'
def id(self): # 获取id值
cursor.execute(self.sql_all)
fin = cursor.fetchall()
if fin:
global id
self.id = fin[-1][0] + 1
else:
self.id = 1
def translate(self):
url='https://fanyi.baidu.com/sug'
headers={'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:95.0) Gecko/20100101 Firefox/95.0'}
while True:
word = input('请输入被翻译的单词或句子(q==quit)')
if word == 'q':
break
data={'kw':word}
re=requests.post(url=url,headers=headers,data=data)
a=re.json()
for i in a['data']:
print(i['k'] , '\t' , i['v'])
def insert(self): # 插入数据
while True:
e = input('请输入英文 (q=quit)')
if e == 'q':
break
c = input('请输入中文')
time1 = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
sql_insert = 'insert into word values(%d,"%s","%s","%s");' % (self.id, e, c, time1)
cursor.execute(sql_insert)
DB.commit()
print(e, c, '以收入数据库')
self.id = self.id + 1
def show(self): # 显示所有单词
cursor.execute(self.sql_all)
fin = cursor.fetchall()
print('序号 英文 中文 时间')
for i in fin:
if i != fin[0]:
print()
for j in i:
print(j,end=' ')
print()
def show_something(self):
a = input('请输入以什么为开头的模糊查找')
sql_something = 'select * from word where english like "{0}%"'.format(a)
#print(sql_something)
print('序号 英文 中文 时间')
cursor.execute(sql_something)
a = cursor.fetchall()
for i in a:
if i != a[0]:
print()
for j in i:
print(j,end=' ')
print()
def delete(self):
a = int(input('请输入单词序号: '))
sql_del = 'delete from word where id={0} '.format(a)
cursor.execute(self.sql_all)
print('序号 英文 中文 时间')
l = cursor.fetchall()
for i in l:
if i[0] == a:
for j in i:
print(j,end=' ')
print()
#print(sql_del)
cursor.execute(sql_del)
DB.commit()
def main():
fun = Func()
fun.id()
print('欢迎来到 <小茅背单词>\n选择功能 1.背单词 2.查翻译 3.显示所有库中单词 4.显示以什么字符开头的单词'
' 5.删除单词 6.开启远程背单词功能 7.q==quit')
while True:
ch = input('请输入序号 (s=show)')
if ch == '1':
fun.insert()
if ch == '2':
fun.translate()
if ch == '3':
fun.show()
if ch == '4':
fun.show_something()
if ch == '5':
fun.delete()
if ch == '6':
import remotely
if ch == 's':
print('欢迎来到 <小茅背单词>\n选择功能 1.背单词 2.查翻译 3.显示所有库中单词 4.显示以什么字符开头的单词'
' 5.删除单词 6.开启远程背单词功能 7.q==quit')
if ch == 'q':
print('Bye')
break
main()
def rec():
import re
Pattern = re.compile(r"^[A-Za-z0-9\u4e00-\u9fa5]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)$")
word=input('word')
if Pattern.match(word):
print('成功')
通过bind绑定ip地址使远程的用户可以连接到remotely
remotely.py
import pymysql
import socket
import time
DB = pymysql.connect(host='localhost', password='1234', charset='utf8', user='root', database='English')
cursor = DB.cursor()
class reFunc():
def __init__(self):
self.sql_all = 'select * from word'
def id(self): # 获取id值
cursor.execute(self.sql_all)
fin = cursor.fetchall()
if fin:
global id
self.id = fin[-1][0] + 1
else:
self.id = 1
def insert(self): # 插入数据
while True:
e = con.recv(1024).decode('utf-8')
if e == 'q':
s.close()
break
c = con.recv(1024).decode('utf-8')
time1 = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
sql_insert = 'insert into word values(%d,"%s","%s","%s");' % (self.id, e, c, time1)
message = '{0},{1}以收入数据库'.format(e, c)
con.send(message.encode('utf-8'))
cursor.execute(sql_insert)
DB.commit()
print(e, c, '以收入数据库')
self.id = self.id + 1
fun = reFunc()
fun.id() # 先获取id值
s = socket.socket()
s.bind(('10.104.45.223', 8888))
print('已准备')
s.listen(5)
con, address = s.accept()
print('连接成功')
message = ('欢迎来到 <小茅背单词>\n 远程功能,你所输入的数据将会存放在主机库中')
con.send(message.encode('utf-8'))
fun.insert()
socket
这是给远程用户使用的,两个用户连接时首先得相互ping通
客户机.py
import socket
s=socket.socket()
s.connect(('10.104.45.223',8888))
rep=s.recv(1024).decode('utf-8')
print(rep)
def select1():
while True:
message=input('请输入英文 q == quit:')
if message == 'q':
s.send(message.encode('utf-8'))
s.close()
break
else:
s.send(message.encode('utf-8'))
message=input('请输入中文')
s.send(message.encode('utf-8'))
rep=s.recv(1024).decode('utf-8')
print(rep)
select1()
Python 英语单词本的更多相关文章
- python实现单词本功能
#实现简单的单词本:# 可以添加单词和词义,当所添加的单词已经存在 让用户知道:# 查找单词,单词不存在时,让用户知道# 删除单词,当删除的单词不存在时,让用户知道# 以上功能无限制操作,直到用户输入 ...
- 小白学习Python英语基础差怎么办,都帮你想好拉!看这里
运算符与随机数 1.module:模块 2.sys(system):系统 3.path:路径 4.import:导入 5.from:从- 定义函数与设定参数 1.birthday:出生日期 2.yea ...
- 还在用背单词App?使用Python开发英语单词自测工具,助你逆袭单词王!
学英语广告 最近也许是刚开学的原因,不管是公众号,还是刷抖音,导出都能看到关于学英语.背单词的广告. 不知道现在学生们背单词买的什么辅导材料.反正我们上学那会,<星火阅读>特别的火.记得当 ...
- python百科
Python 编辑词条 添加义项名 B 添加义项 ? Python(英语发音:/ˈpaɪθən/), 是一种面向对象.解释型计算机程序设计语言,由Guido van Rossum于1989年底发明,第 ...
- Python入门教程(3)
人生苦短,我学Pyhton Python(英语发音:/ˈpaɪθən/), 是一种面向对象.解释型计算机程序设计语言,由Guido van Rossum于1989年底发明,第一个公开发行版发行于199 ...
- Python入门教程(2)
人生苦短,我玩蛇0.0! Python(英语发音:/ˈpaɪθən/), 是一种面向对象.解释型计算机程序设计语言,由Guido van Rossum于1989年底发明,第一个公开发行版发行于1991 ...
- Python入门教程(1)
人生苦短,我用Python! Python(英语发音:/ˈpaɪθən/), 是一种面向对象.解释型计算机程序设计语言,由Guido van Rossum于1989年底发明,第一个公开发行版发行于19 ...
- Python基础入门教程(4)(数据类型)
人生苦短,我学Pyhton Python(英语发音:/ˈpaɪθən/), 是一种面向对象.解释型计算机程序设计语言,由Guido van Rossum于1989年底发明,第一个公开发行版发行于199 ...
- Python基础入门教程(3)
人生苦短,我学Pyhton Python(英语发音:/ˈpaɪθən/), 是一种面向对象.解释型计算机程序设计语言,由Guido van Rossum于1989年底发明,第一个公开发行版发行于199 ...
随机推荐
- 使用Rainbond实现离线环境软件交付
一.离线交付的痛点 在传统行业,如政府.能源.军工.公安.工业.交通等行业,为了防止数据泄露和运行安全考虑,一般情况下网络会采取内外网隔离的策略,以防范不必要的风险,毕竟在安全防护方面,网络物理隔离是 ...
- 如果你不想让pthread_join阻塞你的进程,那么请调用pthread_detach
如果你不想让pthread_join阻塞你的进程,那么请调用pthread_detach 2016年01月13日 16:04:20 炸鸡叔 阅读数:7277 转发自:http://baike.ba ...
- go channel 概述 - 管道
概述 unix/linux OS 的一个进程的输出可以是另一个进程的输入,这些进程使用stdin与stdout设备作为通道,在进程之间传递数据. 同样的,GO中有io.Reader与io.Writer ...
- Oracle trunc和round的区别
1.关于trunc 和round函数比较 整体概括: round函数 四舍五入trunc函数 直接截取 对于时间: Round函数对日期进行"四舍五入",Trunc函数对日期进行截 ...
- Linux_spool命令
spool的作用是什么? spool的作用可以用一句话来描述:在sqlplus中用来保存或打印查询结果. 参数指南 对于SPOOL数据的SQL,最好要自己定义格式,以方便程序直接导入,SQL语句如: ...
- mysql 将A表的a字段赋值给B表的b字段
A.B为2个不同的表,将A表的a字段赋值给B表的b字段:update B set b = (select a from A where A.id = B.id) where B.b is null 注 ...
- 【Service】【Web】【Middleware】Tomcat
1. 概念 1.1. 官方网站:tomcat.apache.org 1.2. tomcat的组件 <Server> <Service> <Connector/> & ...
- Spring Batch Event Listeners
Learn to create and configure Spring batch's JobExecutionListener (before and after job), StepExecut ...
- C# 获取当前目录的父级目录
Directory.GetParent(System.Environment.CurrentDirectory).FullName
- array_filter()用法
第一种情况: 通过函数,过滤数组中的元素 array_filter($arr,'函数名称') 函数里可以写相应的过滤原则,下面举个栗子,过滤掉不是数字的元素 $arr=array('a','b','c ...