Python-commonly used modules

Source: Internet
Author: User

Tag:asc   while   datetime    rules    fat     embedded    float   handler    calculation    

One, logging module 1, log level CRITICAL = #FATAL = CRITICAL ERROR = WARNING = #WARN = WARNING INFO = DEBUG = NOTSET = 0 #不设置2, default level is warning, default print to terminal import logging Logging.debug (' Debug Debug ') logging.info (' message info ') logging.warning (' warning warn ') Logging.error (' bad error ') logging.critical (' critical critical ') output: warning:root: Warning warn error:root: Error "Critical:root: Critical Critical3, specifying a global configuration for the logging module, valid for all logger, control printing to a file you can change the logging module default behavior in the Logging.basicconfig () function by using specific parameters such as the following parameters: FileName: Creates a filedhandler with the specified file name (the concept of handler is explained in the back) so that the log is stored in the specified file. FileMode: File is opened by using this parameter when filename is specified, and the default value is "a" and can be specified as "W". Format: Specifies the log display format used by handler. DATEFMT: Specifies the date time format. Level: Set Rootlogger (The following explains the specific concept) stream: Create Streamhandler with the specified stream. You can specify the output to Sys.stderr,sys.stdout or to a file, and the default is Sys.stderr. If you list both the filename and stream two parameters, the stream parameter is ignored. #格式% (name) S:logger name, not username, Detailed view% (Levelno) S: Log level in digital form% (levelname) S: Log level in text form% (pathname) s: The full pathname of the module that invokes the log output function, may not have% (filename) s: The file name of the module that called the log output function% (module) s: The module name of the call log output function% (funcName) s: function name of the call log output function% (Lineno) d: The code that contains the statement that called the log output functionRow% (created) F: current time, a floating-point number representing the time in Unix standard represents% (relativecreated) d: When the log information is output, the milliseconds since logger was created (Asctime) s: The current time in string form. The default format is "2003-07-08 16:49:45,896". The comma is followed by the millisecond% (thread) d: Thread ID. There may not be a% (ThreadName) s: the thread name. There may not be a% (process) d: Process ID. may not have% (message) s: User output message 4, logging module Formatter,handler,logger,filter object schematic: Logger: The object that produces the log filter: the object that filters the log Handler: Receive logs and then control print to a different place, Filehandler used to print to a file, Streamhandler used to print to the Terminal formatter object: You can customize different log format objects, and then bind to different Handler objects using , in order to control the log format of the different handler 5, Logger and handler level Logger is the first level of filtering, and then to handler, we can give Logger and handler at the same time, but it should be noted that: Logger is also the first to filter the message based on a level-if you set the logger to INFO, and all handlers to DEBUG, you s Till won ' t receive DEBUG messages on Handlers-they ' ll being rejected by the logger itself. If you set logger to debug, and handlers to INFO, you won ' t receive any debug messages Either-because while the Logg Er says "OK, process this", the handlers reject it (DEBUG < INFO). Second, re Module 1, what is a regular? A regular is a combination of symbols with special meanings (called regular expressions) to describe characters or stringsThe method. Or, the regular is the rule used to describe a class of things. (in Python) it is embedded in Python and is implemented through the RE module. The regular expression pattern is compiled into a sequence of bytecode, which is then executed by a matching engine written in C. 2, commonly used matching mode (meta-character) http://blog.csdn.net/yufenghyc/article/details/51078107

    .*默认为贪婪匹配    print(re.findall(‘a.*b‘,‘a1b22222222b‘)) #[‘a1b22222222b‘]    .*?为非贪婪匹配:推荐使用    print(re.findall(‘a.*?b‘,‘a1b22222222b‘)) #[‘a1b‘]3、re模块提供的方法介绍    import re    re.findall    re.search     re.match    re.split    re.sub    re.subn    re.compile三、time与datetime模块1、在Python中,通常有这几种方式来表示时间:    时间戳(timestamp):通常来说,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量。我们运行“type(time.time())”,返回的是float类型。    格式化的时间字符串(Format String)    结构化的时间(struct_time):struct_time元组共有9个元素共九个元素:(年,月,日,时,分,秒,一年中第几周,一年中第几天,夏令时)         import time         #我们先以当前时间为准,让大家快速认识三种形式的时间         print(time.time()) # 时间戳:1487130156.419527         print(time.strftime("%Y-%m-%d %X")) #格式化的时间字符串:‘2017-02-15 11:40:53‘          print(time.localtime()) #本地时区的struct_time         print(time.gmtime())    #UTC时区的struct_time2、计算机认识的时间只能是‘时间戳‘格式,而程序员可处理的或者说人类能看懂的时间有: ‘格式化的时间字符串‘,‘结构化的时间‘,于是有了的转换关系

localtime([secs])将一个时间戳转换为当前时区的struct_time。secs参数未提供,则以当前时间为准。        time.localtime()        time.localtime(1473525444.037215)gmtime([secs]) 和localtime()方法类似,gmtime()方法是将一个时间戳转换为UTC时区(0时区)的struct_time。mktime(t) : 将一个struct_time转化为时间戳。        print(time.mktime(time.localtime()))#1473525749.0 strftime(format[, t]) : 把一个代表时间的元组或者struct_time(如由time.localtime()和time.gmtime()返回)转化为格式化的时间字符串。如果t未指定,将传入time.localtime()。如果元组中任何一个元素越界,ValueError的错误将会被抛出。        print(time.strftime("%Y-%m-%d %X", time.localtime()))#2016-09-11 00:49:56time.strptime(string[, format])把一个格式化时间字符串转化为struct_time。实际上它和strftime()是逆操作。        print(time.strptime(‘2011-05-05 16:37:06‘, ‘%Y-%m-%d %X‘))time.struct_time(tm_year=2011, tm_mon=5, tm_mday=5, tm_hour=16, tm_min=37, tm_sec=6,tm_wday=3, tm_yday=125, tm_isdst=-1)在这个函数中,format默认为:"%a %b %d %H:%M:%S %Y"。

Python-commonly used modules

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.