Python 省市分词提取
来源:互联网 发布:linux 网卡配置网关 编辑:程序博客网 时间:2024/06/04 23:20
第一次使用python编写项目程序,实现地址的省市提取,补全缺省词。已知地址表file1.xls,包含省、市、县名称。从数据库取出的地址通过jieba分词,再与本地表比较,最后将省和市分别列出来。from pandas import *from numpy import *import pandas as pdimport numpy as npimport MySQLdbimport jieba# #建立数据库连接conn = MySQLdb.connect(host="*.*.*.*",user="*",passwd="*",port=0,db="*",charset="utf8")# 通过获取到的数据库连接conn下的cursor()方法来创建游标cursor =conn.cursor()# 解决乱码问题cursor.execute("SET NAMES utf8")# sql语句sql1= "SELECT customer_id,customer_name,address FROM t_customer ORDER BY customer_id limit 50"# read_sql 方法返回的数据类型是DataFramedfSql=pd.read_sql(sql1,con=conn)# 为数据框添加两列dfSql['Province']=''dfSql['City']=''#将省市县数据读取到数据框,本地查询表dfExcel = pd.read_excel('D:\\file\\file1.xls')#遍历从数据库取的数据的每一行for index,dfSqlRows in dfSql.iterrows(): if dfSqlRows[2] is NAN: continue seg_list = list(jieba.cut(str(dfSqlRows[2]), cut_all=False)) #遍历本地地址库的每一行,与当前数据库数据比对 if len(seg_list) > 0: for flag,dfExcelRows in dfExcel.iterrows(): if seg_list[0] in dfExcelRows[2]: dfSql.loc[index, ['Province']] = dfExcelRows[0] dfSql.loc[index, ['City']] = dfExcelRows[1] break elif seg_list[0] in dfExcelRows[1]: dfSql.loc[index, ['Province']] = dfExcelRows[0] dfSql.loc[index, ['City']] = dfExcelRows[1] break elif seg_list[0] in dfExcelRows[0]: dfSql.loc[index, ['Province']] = dfExcelRows[0] if len(seg_list) > 1: if seg_list[1] in dfExcelRows[1] or seg_list[1] in dfExcelRows[2]: dfSql.loc[index, ['City']] = dfExcelRows[1] break else: dfSql.loc[index, ['City']] = "not" else: dfSql.loc[index, ['City']] = "not" break#将数据写到excel中dfSql.to_excel('D:\\file\\省市分类表.xlsx')#关闭游标cursor.close()#关闭数据库连接conn.close()
阅读全文
0 0
- Python 省市分词提取
- python jieba分词(结巴分词)、提取词,加载词,修改词频,定义词库
- 英文分词+提取词干
- 分词关键字提取__jieba
- R软件中jiebaR包分词和用python中jieba分词以及做关键字提取及LDA模型
- python 分词
- 分词工具关键词提取对比
- 分词工具关键词提取对比
- 文件分词以及提取关键词
- python中文分词:结巴分词
- python中文分词,使用结巴分词对python进行分词
- 第1、2章 分词、词干提取
- 【Jieba】json数据处理-提取与分词
- ansj分词算法关键词提取规则
- jieba分词、关键词提取、词性标注
- 基于HanLP分词的命名实体提取
- pyspark+jieba分词 数据标注(提取动词)
- python MMSEG 分词 实现
- JDK-1.8中Stream方法示例
- springmvc 多视图
- vivado学习之PS与PL通信
- JS Crypto函数库使用
- How to overlay an icon over existing shell objects in 3 easy steps
- Python 省市分词提取
- 无法生成.config
- Android EditText控件使用
- 带你测试对比深度学习框架!TensorFlow,Keras,PyTorch...哪家强?(附数据集)
- mysql 递归查询
- 图片的切换
- android note
- 大数据可视化--全球分布散点图
- 高斯消元法(二):高斯消元法原理