Python Road Day5-Common module learning

Source: Internet
Author: User

This section outlines:

    1. Module Introduction
    2. Time &datetime Module
    3. Random
    4. Os
    5. Sys
    6. Shutil
    7. JSON & Picle
    8. Shelve
    9. XML processing
    10. YAML processing
    11. Configparser
    12. Hashlib
    13. Subprocess
    14. Logging module
    15. Re-regular expression

module, which implements a set of code for a function with a mound code.

Like functional programming and process-oriented programming, functional programming accomplishes a function, and other code is used to invoke it, providing reusability of code and coupling between the code. For a complex function, multiple functions may be required to complete (functions can be in different. py files), and the Code collection consisting of N. py files is called a module.

such as: OS is a system-related module; file is a module related to the operation of files

The modules are divided into three types:

    • Custom Modules
    • Built-in standard module (also known as standard library)
    • Open source Module

Reference http://www.cnblogs.com/wupeiqi/articles/4963027.html for the use of custom modules and open source modules

Time & DateTime Module
1#_ *_coding:utf-8_*_2__author__ ='Alex Li'3 4 Import Time5 6 7# Print (Time.clock ()) #返回处理器时间,3. 3 started obsolete, changed to Time.process_time () measurement processor operation time, not including sleep time, unstable, Mac can't be measured8 # Print (time.altzone) #返回与utc时间的时间差, measured in seconds9# Print (Time.asctime ()) #返回时间格式"Fri 11:14:16",Ten # Print (Time.localtime ()) #返回本地时间 the struct time object format One# Print (Time.gmtime (Time.time ()-800000)) #返回utc时间的struc时间对象格式 A  -# Print (Time.asctime (Time.localtime ())) #返回时间格式"Fri 11:14:16", -#print (Time.ctime ()) #返回Fri +  A: -: in  .format, Ibid. the  -  -  - # Date string converted to timestamp +# string_2_struct = Time.strptime ("2016/05/22","%y/%m/%d"#将 Date string into the struct time object format - # Print (string_2_struct) + # # A# Struct_2_stamp =time.mktime (string_2_struct) #将struct时间对象转成时间戳 at # Print (struct_2_stamp) -  -  -  - #将时间戳转为字符串格式 -# Print (Time.gmtime (Time.time ()-86640)) #将utc时间戳转换成struct_time格式 in# Print (Time.strftime ("%y-%m-%d%h:%m:%s", Time.gmtime ())) #将utc struct_time format to the specified string format -  to  +  -  the  * #时间加减 $ Import datetimePanax Notoginseng  -# Print (Datetime.datetime.now ()) #返回 .- ,- +  A: -:03.941925 the#print (Datetime.date.fromtimestamp (Time.time ()) # Timestamp goes directly to date format .- ,- + + # Print (Datetime.datetime.now ()) A# Print (Datetime.datetime.now () + Datetime.timedelta (3)) #当前时间 +3 days the# Print (Datetime.datetime.now () + Datetime.timedelta (-3)) #当前时间-3 days +# Print (Datetime.datetime.now () + Datetime.timedelta (hours=3)) #当前时间 +3 hours -# Print (Datetime.datetime.now () + Datetime.timedelta (minutes= -)) #当前时间 +30 min $  $  - # -# c_time =Datetime.datetime.now () the# Print (C_time.replace (minute=3, hour=2)) #时间替换
View Code
Directive meaning Notes
%a Locale ' s abbreviated weekday name.
%A Locale ' s full weekday name.
%b Locale ' s abbreviated month name.
%B Locale ' s full month name.
%c Locale ' s appropriate date and time representation.
%d Day of the month as a decimal number [01,31].
%H Hour (24-hour clock) as a decimal number [00,23].
%I Hour (12-hour clock) as a decimal number [01,12].
%j Day of the year as a decimal number [001,366].
%m Month as a decimal number [01,12].
%M Minute as a decimal number [00,59].
%p Locale ' s equivalent of either AM or PM. (1)
%S Second as a decimal number [00,61]. (2)
%U Week number of the year (Sunday as the first day of the Week) as a decimal number [00,53]. A New year preceding the first Sunday is considered to is in week 0. (3)
%w Weekday as a decimal number [0 (Sunday), 6].
%W Week number of the year (Monday as the first day of the Week) as a decimal number [00,53]. A New year preceding the first Monday is considered to is in week 0. (3)
%x Locale ' s appropriate date representation.
%X Locale ' s appropriate time representation.
%y Year without century as a decimal number [00,99].
%Y Year with century as a decimal number.
%z Time zone offset indicating a positive or negative time difference from utc/gmt of the form +hhmm or-hhmm, where H repres Ents decimal hour digits and M represents decimal minute digits [-23:59, +23:59].
%Z Time zone name (no characters if no time zone exists).
%% A literal ‘%‘ character.

Random module

Random number

1234 mport randomprintrandom.random()print random.randint(1,2)printrandom.randrange(1,10)

Generate Random Verification code

12345678910 importrandomcheckcode =‘‘fori inrange(4):    current =random.randrange(0,4)    ifcurrent !=i:        temp =chr(random.randint(65,90))    else:        temp =random.randint(0,9)    checkcode +=str(temp)print checkcode

Python Road Day5-Common module learning

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.