Time
t = Time.localtime (Time.time ()-86400)#return local timePrint(T.tm_year, T.tm_mon)#time Stamptime.time ()#time of day before formattingt = Time.localtime (Time.time ()-86400) Time.strftime ('%y/%m/%d%h:%m:%s%W', T)#date strings converted into time objectsTime_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)#converting a Time object into a timestampPrint(Time.mktime (f))#Convert timestamp to string formatPrint(Time.strftime ('%y-%m-%d%h:%m:%s', Time.localtime (Time.time ())))
Datetime
ImportdatetimePrint(Datetime.datetime.now ())#back to 2016-08-19 12:47:03.941925Print(Datetime.date.fromtimestamp (Time.time ()))#The timestamp is converted directly into the date format 2016-08-19Print(Datetime.datetime.now ())Print(Datetime.datetime.now () + Datetime.timedelta (3))#Current time + 3 daysPrint(Datetime.datetime.now () + Datetime.timedelta (-3))#Current Time-3 daysPrint(Datetime.datetime.now () + Datetime.timedelta (hours=3))#Current time + 3 hoursPrint(Datetime.datetime.now () + Datetime.timedelta (minutes=30))#current time +30 minutesDatetime.datetime.now (). Strftime ("%y-%m-%d%h:%m:%s") Time object and string mutual datetime.datetime.fromtimestamp (141456567time object and timestamp Time.mktime (Datetime.datetime.now (). Timetuple ()) Convert to seconds
Random number
ImportRandomPrint('Random floating-point number less than 1:', Random.random ())Print('Random floating-point number in the specified range:', Random.uniform (20,30))Print('a random integer in the specified range:', Random.randint ())Print('gets a random number in the specified step set', Random.randrange (1,10,3))#Step SizePrint(Random.randrange (1,10))#does not contain tenPrint('gets the sequence of the specified length:', Random.sample ([1,2,3,4,5],3))#return to Random list
Zip
Receives multiple sequences as parameters, returns a list of tuples
x = [1, 2, 3= [4, 5, 6,444= [7, 8, 9=# [(1, 4, 7), (2, 5, 8), (3, 6, 9 )]< /c9>x = [1, 2, 3= Zip (* [x] * 3# [( 1, 1, 1), (2, 2, 2), (3, 3, 3)]
String string processing
ImportstringPrint('Lowercase letters :', String.ascii_lowercase)Print('Capital letters:', String.ascii_uppercase)Print('uppercase and lowercase letters:', String.ascii_letters)Print('Number:', String.digits)Print('All symbols:', string.printable)
Serializing JSON and Pickle
- JSON is a form of text storage, pickle is a binary form (at least commonly used binary)
- JSON is human-readable, pickle unreadable
- JSON is widely used in areas other than Python, and pickle is unique to Python.
- JSON only dumps some python built-in objects, and pickle can store almost any object.
in my opinion, the JSON format can be used in favor of applications, especially Web applications. If you prefer algorithmic aspects, especially machine learning, you should use the CPICKLE,PYLEARN2 library to save the model that uses this technology
Has the following objects
data = { 'ID': 1234, 'credit': 15000, 'balance': 8000, ' Create_date'2015-04-04'}
PickleDumps 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) returns: 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 Object (String)-->python object read from file with open ('test' ' RB ' as f: = Pickle.load ( f)print(", ret )
JSONAston
Python Common Modules