Python 常用模組

來源:互聯網
上載者:User

標籤:string   creat   sample   case   with open   tuple   字串   edit   mktime   

 

 

time
t = time.localtime(time.time()-86400)  # 返回本地時間print(t.tm_year, t.tm_mon)# 時間戳記time.time()# 格式化前一天時間t = time.localtime(time.time()-86400) time.strftime(‘%Y/%m/%d %H:%M:%S %W‘, t)# 日期文字轉換成時間對象time_str = ‘2019-11-12 11:11:11‘f = time.strptime(time_str, ‘%Y-%m-%d %H:%M:%S‘)print(f)# time.struct_time(tm_year=2019, tm_mon=11, tm_mday=12, tm_hour=11, tm_min=11, tm_sec=11, tm_wday=1, tm_yday=316, tm_isdst=-1)# 將時間對象轉換成時間戳記print(time.mktime(f))# 將時間戳記轉為字串格式print(time.strftime(‘%Y-%m-%d %H:%M:%S‘,time.localtime(time.time())))

 

datetime
import datetimeprint(datetime.datetime.now()) #返回 2016-08-19 12:47:03.941925print(datetime.date.fromtimestamp(time.time()) )  # 時間戳記直接轉成日期格式 2016-08-19print(datetime.datetime.now() )print(datetime.datetime.now() + datetime.timedelta(3)) #目前時間+3天print(datetime.datetime.now() + datetime.timedelta(-3)) #目前時間-3天print(datetime.datetime.now() + datetime.timedelta(hours=3)) #目前時間+3小時print(datetime.datetime.now() + datetime.timedelta(minutes=30)) #目前時間+30分datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") 時間對象與字串互轉datetime.datetime.fromtimestamp(141456567)  時間對象與時間戳記互轉time.mktime(datetime.datetime.now().timetuple())  轉換成秒

 

random 隨機數
import randomprint (‘小於1的隨機浮點數:‘, random.random())print (‘指定範圍內的隨機浮點數:‘, random.uniform(20,30))print (‘指定範圍內的隨機整數:‘, random.randint(1,2))print (‘按指定步長集合中擷取一個隨機數‘, random.randrange(1,10,3))  # 步長print (random.randrange(1,10))  # 不包含10print(‘擷取指定長度的序列: ‘, random.sample([1,2,3,4,5],3))  # 返回隨機列表

 

zip

接收多個序列作為參數,返回一個元組列表

x = [1, 2, 3]y = [4, 5, 6,444]z = [7, 8, 9]xyz = zip(x, y, z)list(xyz) # [(1, 4, 7), (2, 5, 8), (3, 6, 9)]x = [1, 2, 3]r = zip(* [x] * 3)list(r) # [(1, 1, 1), (2, 2, 2), (3, 3, 3)]

 

string 字串處理
import stringprint(‘小寫字母: ‘,string.ascii_lowercase)print(‘大寫字母: ‘, string.ascii_uppercase)print(‘大寫和小寫字母: ‘, string.ascii_letters)print(‘數字: ‘, string.digits)print(‘所有符號: ‘, string.printable)

 

 

序列化 json與pickle
  • JSON是文本形式的儲存,Pickle則是二進位形式(至少常用二進位)
  • JSON是人可讀的,Pickle不可讀
  • JSON廣泛應用於除Python外的其他領域,Pickle是Python專屬的。
  • JSON只能dump一些python的內建對象,Pickle可以儲存幾乎所有對象。
在我看來,如果偏嚮應用特別是web應用方面,可以常用JSON格式。如果偏向演算法方面,尤其是機器學習,則應該使用cPickle,pylearn2庫中儲存model就是使用這項技術的

 

有如下對象

data = {    ‘id‘: 1234,    ‘credit‘: 15000,    ‘balance‘: 8000,    ‘create_date‘: ‘2015-04-04‘}

 

 

pickle dumps and loads
t = pickle.dumps(data)  # data type is objt2 = pickle.loads(t)  # t type is strprint(‘t: ‘, type(t),t)print(‘t2: ‘, type(t),t2)返回:t:  <class ‘bytes‘> b‘\x80\x03}q\x00(X\x06\x00\x00\x00creditq\x01M\x98:X\x07\x00\x00\x00balanceq\[email protected]\x1fX\x0b\x00\x00\x00create_dateq\x03X\n\x00\x00\x002015-04-04q\x04X\x02\x00\x00\x00idq\x05M\xd2\x04u.‘t2:  <class ‘bytes‘> {‘credit‘: 15000, ‘balance‘: 8000, ‘create_date‘: ‘2015-04-04‘, ‘id‘: 1234}

 

dump and load
with open(‘test‘, ‘wb‘) as f:    pickle.dump(data, f)# pickle對象(字串)-->python對象  從檔案讀取with open(‘test‘, ‘rb‘) as f:    ret = pickle.load(f)    print(‘pickle.load: ‘, ret)

 

json  阿斯頓

 

Python 常用模組

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.