python 替换数据库中文本中特殊字符
来源:互联网 发布:python 2.7.11.tgz 编辑:程序博客网 时间:2024/05/01 10:53
遇见这个一个需求:几百万的数据,需要替换掉不同种类的敏感语句,需要在测试库中进行替换,成功后再替换生产库。
难点1:考虑即适用开发环境,又适用生产环境,考虑使用python(简洁,方便)
难点2:数据量太大;解决办法:查询时使用elasticsearch
难点3:替换种类多;解决办法:进行一定的封装
写的不算细致,可以替换部分规则的敏感语句。代码显示没有格式,需要自行 Reformat Code
具体代码如下:
# -*- coding: UTF-8 -*-
from elasticsearch import Elasticsearch
import pymysql
import re
import time
# Elasticsearch查找query匹配的结果
def getElasticsearchList(query_string=''):
es = Elasticsearch(
[
'http://192.168.1.166:9200',
]
)
res = es.search(index="test-questions", body={"query": {
"bool": {"must": [{"query_string": {"default_field": "test-question.content", "query": query_string}}],
"must_not": [],
"should": []}}, "from": 0, "size": 2})
print("Got %d Hits:" % res['hits']['total'])
# 查找匹配的字符 ,并以字典形式{id:content}返回
dict = {}
for hit in res['hits']['hits']:
dict[hit["_source"]['id']] = hit["_source"]['content']
return dict
#替换符合规则的字符
def deleteString(dict):
result = {}
for key in dict:
reg = r'alt=".*!"' #替换'alt'标签
strre = re.compile(reg)
str = re.sub(strre, ' ', dict[key])
result[key] = str
return result
#更新操作
conn = pymysql.connect(host='192.168.1.225', user='admin', passwd='neptune@admin', db='teachresource', port=3306,
charset='utf8')
cur = conn.cursor()
resultList = deleteString(dict)
for key in resultList:
cmd = "update sh_testquestion_bak set updateTime=%s,content =%s where id=%s"
cur.execute(
cmd, (int(time.time()) * 1000, resultList[key], key))
conn.commit()
conn.close()
- python 替换数据库中文本中特殊字符
- python--替换特殊字符
- python替换UTF-8编码文本中任意特殊字符,包括中文符号
- python替换UTF-8编码文本中任意特殊字符,包括中文符号问题:大量文本,将其中的特殊字符用空
- word中查找和替换特殊字符
- 标签中文本某些字符替换(字符转换)
- 特殊字符替换过滤器
- java特殊字符替换
- java 替换特殊字符
- 替换html特殊字符
- java特殊字符替换
- js特殊字符替换
- 正则替换特殊字符
- PHP替换特殊字符
- 替换特殊字符
- C#中把中文字符替换为*
- eclipse中搜索替换所有中文字符
- 替换表中所有中文字符
- C++中指针与C#中引用类型传递相似性
- 集合框架_Collection存储自定义对象并遍历案例
- easyui 自适应 fit属性
- Json解析,FastJson解析
- 三国佚事——巴蜀之危
- python 替换数据库中文本中特殊字符
- php 安装xdebug扩展
- javascript数据类型
- 指针作为返回值
- Kali2.0 update到最新版本后安装shadowsocks服务报错问题
- 嵌入式的童年-第19课:安装Linux系统到开发板
- pod报错
- bzoj4009 [HNOI2015]接水果
- 如何用两个栈实现队列的功能