程序分为三个功能块
1、sql() 数据库功能块,这个是下载网上源代码,自己加了些东西,其源码来源于此处点击打开链接。
2、txt()文本处理模块,搜寻单词,对比本地数据库,不在本地数据库中单词,去有道翻译,得到翻译,去掉人名,地名,或者短语,写入到数据库,并写入到txt文件(上传用)。
3、url()翻译模块。
另外做了数据库,从网上下载的单词txt文件,转入数据库中,库中表包括四级和六级。
#!/usr/bin/python
#-*- coding: utf-8 -*-
import os
import sys
import sqlite3
import time
import codecs
import urllib.request
import urllib.parse
import json
class sql(object):
'''test.db中有三个表,第一个是cet_4, 第二个是conquer_word, 第三个是temp, 第四个cet_6
id integer integer integer integer
en text text text text
cn text real text text
add_time text text text text
conquer INT int int int
classify .. .. .. text '''
def __init__(self):
'''初始化方法'''
#数据库文件绝句路径
global DB_FILE_PATH
DB_FILE_PATH = os.getcwd()+'\\test.db'
#数据库表名称
global TABLE_NAME
TABLE_NAME = 'cet_4'
#是否打印sql
global SHOW_SQL
SHOW_SQL = True
print('show_sql : {}'.format(SHOW_SQL))
'''#self.conn=sqlite3.connect(path)#test.db####只能初始调用,本打算去掉get_conn功能
#self.cursor=self.conn.cursor()'''
def get_conn(self,path):
'''获取到数据库的连接对象,参数为数据库文件的绝对路径
如果传递的参数是存在,并且是文件,那么就返回硬盘上面改
路径下的数据库文件的连接对象'''
conn = sqlite3.connect(path)#test.db
if os.path.exists(path) and os.path.isfile(path):
return conn
def get_cursor(self,conn):
'''该方法是获取数据库的游标对象,参数为数据库的连接对象
如果数据库的连接对象不为None,则返回数据库连接对象所创
建的游标对象;否则返回一个游标对象,该对象是内存中数据
库连接对象所创建的游标对象'''
if conn is not None:
return conn.cursor()
else:
return self.get_conn('').cursor()
def create_table(self, sql):
'''创建数据库表:'''
if sql is not None an