python数据类型入门(一)之字符串、布尔类型和日期时间
【摘要】
目录
1、字符串
2、布尔类型
3、日期和时间
3.1、获取当前时间
3.2、获取格式化的时间
3.3、获取日期差
3.4、获取时间差
3.5、获取上个月最后一天
3.6、字符串日期格式化为秒数
3.7、日期格式化为秒数
3.8、秒数转字符串
1、字符串
1.1、如何在Python中使用字符串
a、使用...
目录
1、字符串
1.1、如何在Python中使用字符串
a、使用单引号(')
用单引号括起来表示字符串,例如:
str='this is string';
print str;
b、使用双引号(")
双引号中的字符串与单引号中的字符串用法完全相同,例如:
str="this is string";
print str;
c、使用三引号(''')
利用三引号,表示多行的字符串,可以在三引号中自由的使用单引号和双引号,例如:
str='''this is string
this is pythod string
this is string'''
print str;
2、布尔类型
bool=False;
print bool;
bool=True;
print bool;
3、日期和时间
3.1、获取当前时间
例如:
import time, datetime;
#当前时间:
localtime = time.localtime(time.time())
print "Local current time :", localtime
## 结构化时间
time.struct_time(tm_year=2014, tm_mon=3, tm_mday=21, tm_hour=15, tm_min=13, tm_sec=56, tm_wday=4, tm_yday=80, tm_isdst=0)
说明:time.struct_time(tm_year=2014, tm_mon=3, tm_mday=21, tm_hour=15, tm_min=13, tm_sec=56, tm_wday=4, tm_yday=80, tm_isdst=0)属于struct_time元组,struct_time元组具有如下属性:
3.2、获取格式化的时间
可以根据需求选取各种格式,但是最简单的获取可读的时间模式的函数是asctime():
1、日期转换为字符串
首选:print time.strftime('%Y-%m-%d %H:%M:%S');
其次:print datetime.datetime.strftime(datetime.datetime.now(), '%Y-%m-%d %H:%M:%S')
最后:print str(datetime.datetime.now())[:19]
2、字符串转换为日期
expire_time = "2013-05-21 09:50:35"
d = datetime.datetime.strptime(expire_time,"%Y-%m-%d %H:%M:%S")
print d;
3.3、获取日期差
## 定义时差步长
oneday = datetime.timedelta(days=1)
#今天,2014-03-21
today = datetime.date.today()
#昨天,2014-03-20
yesterday = datetime.date.today() - oneday
#明天,2014-03-22
tomorrow = datetime.date.today() + oneday
#获取今天零点的时间,2014-03-21 00:00:00
today_zero_time = datetime.datetime.strftime(today, '%Y-%m-%d %H:%M:%S')
#0:00:00.001000
print datetime.timedelta(milliseconds=1), #1毫秒
#0:00:01
print datetime.timedelta(seconds=1), #1秒
#0:01:00
print datetime.timedelta(minutes=1), #1分钟
#1:00:00
print datetime.timedelta(hours=1), #1小时
#1 day, 0:00:00
print datetime.timedelta(days=1), #1天
#7 days, 0:00:00
print datetime.timedelta(weeks=1)
3.4、获取时间差
#1 day, 0:00:00
oneday = datetime.timedelta(days=1)
#今天,2014-03-21 16:07:23.943000
today_time = datetime.datetime.now()
#昨天,2014-03-20 16:07:23.943000
yesterday_time = datetime.datetime.now() - oneday
#明天,2014-03-22 16:07:23.943000
tomorrow_time = datetime.datetime.now() + oneday
注意时间是浮点数,带毫秒。
那么要获取当前时间,需要格式化一下:
print datetime.datetime.strftime(today_time, '%Y-%m-%d %H:%M:%S')
print datetime.datetime.strftime(yesterday_time, '%Y-%m-%d %H:%M:%S')
print datetime.datetime.strftime(tomorrow_time, '%Y-%m-%d %H:%M:%S')
3.5、获取上个月最后一天
last_month_last_day = datetime.date(datetime.date.today().year,datetime.date.today().month,1)-datetime.timedelta(1)
3.6、字符串日期格式化为秒数
返回浮点类型:
expire_time = "2013-05-21 09:50:35"
d = datetime.datetime.strptime(expire_time,"%Y-%m-%d %H:%M:%S")
time_sec_float = time.mktime(d.timetuple())
print time_sec_float
3.7、日期格式化为秒数
返回浮点类型:
d = datetime.date.today()
time_sec_float = time.mktime(d.timetuple())
print time_sec_float
3.8、秒数转字符串
time_sec = time.time()
print time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time_sec))
文章来源: zhulin1028.blog.csdn.net,作者:zhulin1028,版权归原作者所有,如需转载,请联系作者。
原文链接:zhulin1028.blog.csdn.net/article/details/124139938
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
作者其他文章
评论(0)