Python 常用
导出虚拟环境 安装文件中虚拟环境
pip freeze > requirements.txt
pip install -r requirements.txt
Python使用镜像源
pip install -i https://mirrors.aliyun.com/pypi/simple/
Python列表内字典去重
def list_dict_set(list_dict_data):
"""
列表内字典去重
:param list_dict_data:
:return:
"""
run_function = lambda x, y: x if y in x else x + [y]
return reduce(run_function, [[], ] + list_dict_data)
Python插入数据库方法
def database_insert(sql, value):
"""
插入数据库方法
:param sql:replace into 表名(字段1,字段2)value(%s,%s)
:param value:[(1,2),(2,3)]
:return:
"""
db = pymysql.connect(host='', user='', password=,
port=3306, db='data_center')
cursor = db.cursor()
try:
cursor.executemany(sql, value)
db.commit()
print('插入数据成功')
except Exception as e:
db.rollback()
print("插入数据失败", e)
finally:
db.close()
Python字典合并
def Merge(dict1, dict2):
"""
字典合并
:param dict1:
:param dict2:
:return:
"""
(dict2.update(dict1))
d = dict2
return d
Python字典 按照key排序
def dict_sort(params: dict):
"""
字典key排序
"""
param = dict()
params = sorted(params.items(), key=lambda x: x[0])
for k, v in params:
param[k] = v
return param
Python字符串日期转换时间戳
def str_date_to_num(str_data):
"""
字符串日期转换时间戳
:param str_data: 2021-06-01 00:00:00
:return:1622476800
"""
strptime = time.strptime(str_data, "%Y-%m-%d %H:%M:%S")
mktime = int(time.mktime(strptime))
return mktime
Python时间戳转换字符串
def timeStamp(timeNum):
"""
时间戳转换字符串
:param timeNum: 163042560000
:return:1975-03-03 09:36:00
"""
if timeNum is None or timeNum == 0:
return None
timeStamp = float(timeNum/1000)
timeArray = time.localtime(timeStamp)
otherStyleTime = time.strftime("%Y-%m-%d %H:%M:%S", timeArray)
return otherStyleTime
Python上传文件到服务器
def ssh_scp_put(ip, username, password, local_file, remote_path):
"""
上传文件到服务器
:param ip:
:param username:
:param password:
:param local_file:
:param remote_path:
:return:
"""
ssh = paramiko.SSHClient()
ssh.load_system_host_keys()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname=str(ip), port=22, username=username, password=password)
scp = SCPClient(ssh.get_transport())
scp.put(local_file, recursive=True, remote_path=remote_path)
Python 刷新Excel内数据透视表
def refresh_excel(path):
"""
刷新excel内数据透视表数据,前提是excel内数据透视表需要打开 数据透视表选项-》数据-》打开文件时自动刷新
:param path:
:return:
"""
application = win32com.client.Dispatch("Excel.Application")
workbook = application.Workbooks.Open(path)
application.Visible = True
application.AskToUpdateLinks = False
workbook.RefreshAll()
application.AskToUpdateLinks = True
workbook.Save()
workbook.Close(True)
application.Quit()
Python des加密 解密函数
def _encrypt(data):
key = '密钥'
des = pyDes.des(key, pyDes.ECB, b"\0\0\0\0\0\0\0\0", pad=None, padmode=pyDes.PAD_PKCS5)
ecryptdata = des.encrypt(data)
return bytes.decode(base64.b64encode(ecryptdata))
def _decrypt(ecryptdata):
key = '密钥'
des = pyDes.des(key, pyDes.ECB, b"\0\0\0\0\0\0\0\0", pad=None, padmode=pyDes.PAD_PKCS5)
data = des.decrypt(base64.b64decode(ecryptdata))
print(bytes.decode(data))
return bytes.decode(data)
Ubuntu 常用
更新
sudo apt-get update
更新已安装的包
sudo apt-get upgrade
给xxx.sh 执行权限
chmod +x xxx.sh
传输文件
打开cmd窗口输入:
scp -r 文件路径/文件名or文件夹名 账号@ubuntu地址:ubuntu内存放地址
如:
scp -r D:\datas\xxx.xlsx root@123.123.123.123:/home/data/
scp -r 账号@ubuntu地址:ubuntu内文件or文件夹地址 本地存放位置
如:
scp -r root@123.123.123.123:/home/data/xxx.xlsx D:/datas/
nohup 程序运行指令 -p 端口号 >日志地址 2>&1 &
如:nohup airflow webserver -p 80 >/home/aw.log 2>&1 &
Mysql 基本操作
mysql -h host -P port -uuser -ppwd
truncate table 表名
添加primary key(主键索引)
alter table 表名 add primary key(列名);
添加unique(唯一索引)
alter table 表名 add unique(列名);
添加index(普通索引)
alter table 表名 add index 索引名 (列名);
添加fulltext(全文索引)
alter table 表名 add fulltext (列名);
添加多列索引
alter table 表名 add index 索引名(index_name) (列名1,列名2.......);
当不再需要索引时,可以使用 DROP INDEX 语句来对索引进行删除。
DROP INDEX <索引名> ON <表名>
select
table_schema as '数据库',
table_name as '表名',
table_rows as '记录数',
concat(data_length/1024/1024, 2) as '数据容量(MB)',
concat(index_length/1024/1024, 2) as '索引容量(MB)'
from information_schema.tables
where table_schema='shopee' and table_name like '%search_by_catid'
order by data_length desc, index_length desc;
CREATE TABLE `表名` (
`updatetime` datetime not null,
`itemid` bigint NULL DEFAULT NULL,
`shopid` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
`id` int NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id`,updatetime) USING BTREE,
INDEX `idx_updatetime`(`updatetime`) USING BTREE,
INDEX `idx_itemid`(`itemid`) USING BTREE
) ENGINE = InnoDB
PARTITION BY RANGE(YEAR(updatetime))(
partition p2021 values less than (2022),
partition p2022 values less than (2023),
partition p2023 values less than (2024),
partition p2024 values less than (2025),
SELECT table_name,partition_name,partition_description,table_rows
FROM information_schema.PARTITIONS
WHERE table_name='表名';
Redis 基本操作
D:\jet>redis-cli
127.0.0.1:6379> exists 99
(integer) 1
127.0.0.1:6379> type zz
string
127.0.0.1:6379> get zz
"99"
127.0.0.1:6379> append zz 呀
(integer) 4
127.0.0.1:6379> get zz
"99呀"
127.0.0.1:6379> keys z*
1) "zz"
2) "zzz"
3) "zzzz"
127.0.0.1:6379> randomkey
"zzz"
127.0.0.1:6379> exists azz
(integer) 0
127.0.0.1:6379> rename zzz azz
OK
127.0.0.1:6379> exists azz
(integer) 1
127.0.0.1:6379> expire azz 10
(integer) 1
127.0.0.1:6379> ttl azz
(integer) 3
127.0.0.1:6379> exists azz
(integer) 0
127.0.0.1:6379> keys z*
1) "zz"
2) "zzzz"
127.0.0.1:6379> del zzzz
(integer) 1
127.0.0.1:6379> keys z*
1) "zz"
Other
后续会持续更新。大家觉得还有什么常用到的可以在下方评论,我会持续增加。