Python學習筆記一 Python中的time

2021-09-02 07:22:01 字數 1993 閱讀 6638

返回時間戳,即

從2023年1月1日午夜(曆元)到今日經過了的秒數。

import time;  # 引入time模組

ticks = time.time() #time()函式用來返回時間戳

print "當前時間戳為:", ticks

執行結果當前時間戳為: 1459994552.51

python用乙個含有九個數字的元組來表示時間,這個元組是struct_time元組

這個函式用來將乙個時間戳格式化,返回乙個時間元組,表示本地時間

import time

localtime = time.localtime(time.time())

print "本地時間為 :", localtime

執行結果:本地時間為 : time.struct_time(tm_year=2016, tm_mon=4, tm_mday=7, tm_hour=10, tm_min=3, tm_sec=27, tm_wday=3, tm_yday=98, tm_isdst=0)

這個函式,將乙個時間元組格式化成特定格式的字串,引數是乙個時間元組,返回格式化的時間

# -*- coding: utf-8 -*-

import time

localtime = time.asctime( time.localtime(time.time()) )

print "本地時間為 :", localtime

執行結果本地時間為 : thu apr 7 10:05:21 2016

這個函式接收乙個指定格式的字串,和乙個時間元組,將這個時間元組代表的時間,以指定的格式返回

import time

# 格式化成2016-03-20 11:45:39形式

print time.strftime("%y-%m-%d %h:%m:%s", time.localtime())

# 格式化成sat mar 28 22:24:24 2016形式

print time.strftime("%a %b %d %h:%m:%s %y", time.localtime())

執行結果

2018-11-30 21:19:38

fri nov 30 21:19:38 2018
import time

a = "sat mar 28 22:24:24 2016"

print time.strptime(a, "%a %b %d %h:%m:%s %y")

執行結果

time.struct_time(tm_year=2016, tm_mon=3, tm_mday=28, tm_hour=22, tm_min=24, tm_sec=24, tm_wday=5, tm_yday=88, tm_isdst=-1)
import time

a = "sat mar 28 22:24:24 2016"

print time.strptime(a, "%a %b %d %h:%m:%s %y")

print time.mktime(time.strptime(a,"%a %b %d %h:%m:%s %y"))

結果

time.struct_time(tm_year=2016, tm_mon=3, tm_mday=28, tm_hour=22, tm_min=24, tm_sec=24, tm_wday=5, tm_yday=88, tm_isdst=-1)

1459175064.0

怎樣學習python語言 如何學習python語言

python語言很神秘嗎 python作為一款類自然語言的程式語言,很方便的就可以上手,而且可以說是老少皆宜。用python程式設計非常簡單,只要有一台電腦 無論windows或者蘋果 就可以完成python程式設計。完成python程式設計首先需要在電腦上安裝python環境,安裝完成後就可以開始...

深圳python費用標準 深圳學習python學費

第 一步 基本的 python 技能 首先要安裝 python 由於我們要使用機器學習和科學計算的 packages 這裡建議安裝 anaconda。anaconda 是乙個可在 linux osx windows 上執行的 python 實現工具,擁有所需的機器學習 packages 包括 num...

Python學習 學習筆記(一)

python是什麼?人們為和使用python python的缺點 如今誰在使用python 流行的p2p檔案分享系統bitjorrent是乙個python程式。eve online這款大型多人網路遊戲 massively multiplayer online game,mmog 廣泛地使用pytho...