佛系补充,随缘记录,欢迎留言补充
一、时间处理
(0)导包
from datetime import datetime, timedelta
(1)每日任务获取前一天时间
now = datetime.now()
end = datetime(now.year, now.month, now.day, 00)
start = end - timedelta(days=1)
(2)从外部输入时间:输入参数获取、字符串转时间、时间转字符串
import sys
if( len(sys.argv) > 1):
start=datetime.strptime(sys.argv[1],'%Y-%m-%d')
print(start.strftime("%Y-%m-%d"))
二、数据库
import mysql.connector
conn = mysql.connector.connect(host='', port=3306, user='', passwd='', charset="utf8")
cursor = conn1.cursor()
select="select count(*),sum(fee) from tables_%s; "
count=0;
fee=0;
for i in range(0,100):
#填充成两位
db=str(i).zfill(2)
cursor.execute(select % db)
results=cursor.fetchall()
count+=results[0][0]
fee+=results[0][1]
print(str(count),str(fee/100))
cursor.close()
conn.close()
三、读文件
#读取文件中每一行的每个值
f=open("data.txt",'r')
for data in f.readlines():
result=data.split()
print(result[0],result[1])
f.close()
四、执行
PYTHONIOENCODING=utf-8 /home/python3/bin/python3 xxx.py
#crontab -e 执行时解决中文乱码并生成日志
00 * * * * cd /home/data && PYTHONIOENCODING=utf-8 /home/python3/bin/python3 /home/data/deal.py >> deal.log 2>&1