python 技术大杂烩

python2.7 下载文件

os.chdir(“目标目录”) #修改当前工作目录
os.getcwd() #获取当前工作目录

import os,urllib2,urllib

url = json.loads(response)[‘Data’][‘DownloadUrl’]
print(url)
urllib.urlretrieve(url , ‘./test.xls’)

python 打开url 包含中文
import urllib.request
import string
from urllib.parse import quote
url = ‘http://xxx/type=%s’%(中文)
print(url)
s = quote(url,safe=string.printable)
f = urllib.request.urlopen(s).read()

修改临时 使用 pip 源
sudo ./pip-2.7 install aliyun-python-sdk-core -i http://mirrors.aliyun.com/pypi/simple/

修改默认pip源
vi ~/.pip/pip.conf

[global]
index-url = http://mirrors.aliyun.com/pypi/simple/

1542556800000
时间------------

time.time()

秒转化为时间
time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(1541411593))
time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(1542556800))

当前时间
time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())

取几天前 几小时 几分钟 几周前时间
print ((datetime.datetime.now()-datetime.timedelta(minutes=2)).strftime("%Y-%m-%d %H:%M"))
days、seconds、minutes、hours、weeks等

获取周一时间
today = datetime.date.today()
print(today - datetime.timedelta(today.weekday()))

today = datetime.date.today()        # 获取当前日期, 因为要求时分秒为0, 所以不要求时间
weekday = today.weekday()        # 获取当前周的排序, 周一为0, 周日为6
monday_delta = datetime.timedelta(weekday)    # 当前日期距离周一的时间差
sunday_delta = datetime.timedelta(7 - weekday)    # 当前日期距离下周一的时间差
monday = today - monday_delta    # 获取这周一日期
next_monday = today + sunday_delta    # 获取下周一日期

取小数位后2位
round(99.2313242,2)

读excel
sudo pip install xlutils

import datetime,calendar
/*星期日:Calendar.SUNDAY=1
*星期一:Calendar.MONDAY=2
*星期二:Calendar.TUESDAY=3
*星期三:Calendar.WEDNESDAY=4
*星期四:Calendar.THURSDAY=5
*星期五:Calendar.FRIDAY=6
*星期六:Calendar.SATURDAY=7 */

获取上周日

(datetime.datetime.today() - datetime.timedelta(days=time.localtime().tm_wday + 1)).strftime("%Y-%m-%d")

获取上周一

(datetime.datetime.today() - datetime.timedelta(days=time.localtime().tm_wday + 7)).strftime("%Y-%m-%d")

time.localtime().
int tm_sec; /* 秒 – 取值区间为[0,59] /
int tm_min; /
分 - 取值区间为[0,59] /
int tm_hour; /
时 - 取值区间为[0,23] /
int tm_mday; /
一个月中的日期 - 取值区间为[1,31] /
int tm_mon; /
月份(从一月开始,0代表一月) - 取值区间为[0,11] /
int tm_year; /
年份,其值等于实际年份减去1900 /
int tm_wday; /
星期 – 取值区间为[0,6],其中0代表星期天,1代表星期一,以此类推 /
int tm_yday; /
从每年的1月1日开始的天数 – 取值区间为[0,365],其中0代表1月1日,1代表1月2日,以此类推 /
int tm_isdst; /
夏令时标识符,实行夏令时的时候,tm_isdst为正。不实行夏令时的时候,tm_isdst为0;不了解情况时,tm_isdst()为负。

import datetime
from datetime import timedelta
now = datetime.datetime.now()
#今天
today = now
#昨天
yesterday = now - timedelta(days=1)
#明天
tomorrow = now + timedelta(days=1)

#当前季度
now_quarter = now.month / 3 if now.month % 3 == 0 else now.month / 3 + 1
#本周第一天和最后一天
this_week_start = now - timedelta(days=now.weekday())
this_week_end = now + timedelta(days=6-now.weekday())
#上周第一天和最后一天
last_week_start = now - timedelta(days=now.weekday()+7)
last_week_end = now - timedelta(days=now.weekday()+1)
#本月第一天和最后一天
this_month_start = datetime.datetime(now.year, now.month, 1)
this_month_end = datetime.datetime(now.year, now.month + 1, 1) - timedelta(days=1)
#上月第一天和最后一天
last_month_end = this_month_start - timedelta(days=1)
last_month_start = datetime.datetime(last_month_end.year, last_month_end.month, 1)
#本季第一天和最后一天
month = (now.month - 1) - (now.month - 1) % 3 + 1
this_quarter_start = datetime.datetime(now.year, month, 1)
this_quarter_end = datetime.datetime(now.year, month + 3, 1) - timedelta(days=1)
#上季第一天和最后一天
last_quarter_end = this_quarter_start - timedelta(days=1)
last_quarter_start = datetime.datetime(last_quarter_end.year, last_quarter_end.month - 2, 1)
#本年第一天和最后一天
this_year_start = datetime.datetime(now.year, 1, 1)
this_year_end = datetime.datetime(now.year + 1, 1, 1) - timedelta(days=1)

#去年第一天和最后一天
last_year_end = this_year_start - timedelta(days=1)
last_year_start = datetime.datetime(last_year_end.year, 1, 1)

猜你喜欢

转载自blog.csdn.net/haogeoyes/article/details/83987657