未验证 提交 cbfe8465 编写于 作者: 片刻小哥哥's avatar 片刻小哥哥 提交者: GitHub

Merge pull request #2 from jiangzhonglian/master

修改 README
# Middleware
中间件 - 日常小工具
* [Python – 如何通过多个字符替换(replace)字符串的方法](doc/mul_replace.md)
* [Python – 如何通过多个字符分割(split)字符串的方法](doc/mul_split.md)
# Python – 如何通过多个字符替换(replace)字符串的方法
> 方式1:
```py
a = "eew \\ eawr,2 fd sa:21"
b = a.replace("\\", " ").replace(":", " ").replace(",", " ")
print(b)
```
> 方式2:
```
import re
a = "eew \\ eawr,2 fd sa:21"
b = re.sub(r"[:,\\ ]+", " ", a) # 前面是正则表达式,匹配多种字符(串)
print(b)
```
***
原文:https://blog.csdn.net/liuchengzimozigreat/article/details/85339372
# python中字符串自带的split方法一次只能使用一个字符对字符串进行分割,但是python的正则模块则可以实现多个字符分割
```py
import re
re.split('[_#|]','this_is#a|test')
```
返回的是一个列表(list),输出结果如下:
```
['this', 'is', 'a', 'test']
```
***
原文:https://blog.csdn.net/yockie/article/details/48298343
# coding: utf8
from u_tool.text import *
#!/usr/bin/python
# coding:utf-8
# -------------------------------------------------------------------------------
# Name: 爬取微博的数据
# Purpose: 找出相近的词
# Author: jiangzhonglian
# Create_time : 2018年9月14日
# Update_time: 2018年9月14日
# Content:
# Copyright: (c) jiangzhonglian 2018
# Licence: <do yourself>
# -------------------------------------------------------------------------------
from pymysql import *
class MySQL:
def __init__(self,
database,
host="127.0.0.1",
user="xxx",
password="xxx",
port=3306,
charset="utf8"):
self.host = host
self.user = user
self.password = password
self.port = port
self.database = database
self.charset = charset
# 数据库连接方法:
def open(self):
self.db = connect(
host=self.host,
user=self.user,
password=self.password,
port=self.port,
database=self.database,
charset=self.charset)
# 游标对象
self.cur = self.db.cursor()
# 数据库关闭方法:
def close(self):
self.cur.close()
self.db.close()
# 数据库执行操作方法:
def execute(self, sql, L=[]):
try:
self.open()
self.cur.execute("%s;" % sql, L)
self.db.commit()
print("ok")
msg = "success"
except Exception as e:
# 错误回滚
self.db.rollback()
print("Failed", e)
msg = "fail"
finally:
self.close()
# 返回统一状态
return msg
# 数据库查询所有操作方法:
def execute_all(self, sql, L=[]):
try:
self.open()
self.cur.execute("%s;" % sql, L)
self.cur.fetchall()
print("ok")
msg = "success"
except Exception as e:
# 错误回滚
self.db.rollback()
print("Failed", e)
msg = "fail"
finally:
self.close()
# 返回统一状态
return msg
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册