zoukankan      html  css  js  c++  java
  • 文本处理实例

    三个脚本文件

    ①、创建表

    #创建表

    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)
     
  • 相关阅读:
    Redis简介(数据结构,哨兵、集群和SpringDataRedis)
    FastDFS + Nginx
    Dubbo
    Ubuntu下Zookeeper的安装和基本使用
    RMI简介
    RPC
    各大技术团队的博客
    天了噜,为什么外链css要放在头部,js要放在尾部?
    月经帖 | 2019.11
    前端面试知识点
  • 原文地址:https://www.cnblogs.com/Jweiqing/p/9113593.html
Copyright © 2011-2022 走看看