文本处理实例

三个脚本文件

①、创建表

#创建表

from sqlalchemy import Column, MetaData, Table
from sqlalchemy import Integer
from sqlalchemy import String
from sqlalchemy import create_engine


engine = create_engine('mysql+pymysql://jiang:123456@192.168.137.130/data')

metadata = MetaData(engine)

dictionary = Table('dictionary', metadata,
Column('id', Integer, primary_key=True),
Column('key', String(50)),
Column('value', String(50))
)
student = Table('student', metadata,
Column('id', Integer, primary_key=True),
Column('name', String(20)),
Column('gender', String(2)),
Column('age', String(5))
)
metadata.create_all(engine)

②、将处理完的数据插入表中

#在表中插入数据

import codecs
from sqlalchemy import create_engine, Column, Integer, String
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker

engine = create_engine('mysql+pymysql://jiang:123456@192.168.137.130:3306/data?charset=utf8')
DBSession = sessionmaker(bind=engine)
session = DBSession()
Base = declarative_base()


class InsertDatas(Base):
__tablename__= 'dictionary'
__table_args__ = {
        'mysql_engine': 'InnoDB', #选择InnoDB格式
        'mysql_charset': 'utf8mb4' #选择utf8格式
    }


id = Column(Integer, primary_key=True)
key = Column(String(50))
value = Column(String(50))

#处理数据并插入表中
class OptDate(object):
def __init__(self, datafile):
self.datafile = datafile

def makedatatostr(self):
with codecs.open("dictionary.txt", encoding='utf-8') as file:
for (num, value) in enumerate(file):
line = value.strip().split()
diction = InsertDatas(id=num+1, key=line[0], value=line[1])
session.add(diction)
session.commit()


if __name__ == '__main__':
optdate = OptDate("dictionary.txt")
optdate.makedatatostr()
session.close()

③、查询表中的数据

#查询数据

from sqlalchemy import create_engine, Column, Integer, String
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker

engine = create_engine('mysql+pymysql://jiang:123456@192.168.137.130:3306/data?charset=utf8')
DBSession = sessionmaker(bind=engine)
session = DBSession()
Base = declarative_base()


class InsertDatas(Base):
__tablename__= 'dictionary'
id = Column(Integer, primary_key=True)
key = Column(String(50))
value = Column(String(50))

a = session.query(InsertDatas).filter(InsertDatas.id<10).all()
for i in a:
print(i.id)
print(i.key)
print(i.value)

word = input("please input your a word:")
result =session.query(InsertDatas).filter(InsertDatas.key.like("%{0}%".format(word))).all() #查询和输入有关的词
for each in result:
print(each.id, each.key, each.value)
 

 

posted @ 2018-05-30 21:25  一条咸鱼的梦想  阅读(194)  评论(0编辑  收藏  举报