• Day 16 Frequently-used modules


    day16思维导图

    一 Regular Expression

    A RegEx, or Regular Expression, is a sequence of characters that forms a search pattern.

    RegEx can be used to check if a string contains the specified search pattern.

    Metacharacters元字符

    Metacharacters are characters with a special meaning

    CharacterDescriptionExample
    [] A set of characters "[a-m]"
    Signals a special sequence (can also be used to escape special characters) "d"
    . Any character (except newline character) "he..o"
    ^ Starts with "^hello"
    $ Ends with "world$"
    * Zero or more occurrences "aix*"
    + One or more occurrences "aix+"
    {} Exactly the specified number of occurrences "al{2}"
    | Either or "falls|stays"
    () Capture and group  

     

    Special Sequences

    A special sequence is a followed by one of the characters in the list below, and has a special meaning:

    CharacterDescriptionExample
    A Returns a match if the specified characters are at the beginning of the string "AThe"
     Returns a match where the specified characters are at the beginning or at the end of a word (the "r" in the beginning is making sure that the string is being treated as a "raw string")C r"ain" r"ain"
    B Returns a match where the specified characters are present, but NOT at the beginning (or at the end) of a word (the "r" in the beginning is making sure that the string is being treated as a "raw string") r"Bain" r"ainB"
    d Returns a match where the string contains digits (numbers from 0-9) "d"
    D Returns a match where the string DOES NOT contain digits "D"
    s Returns a match where the string contains a white space character "s"
    S Returns a match where the string DOES NOT contain a white space character "S"
    w Returns a match where the string contains any word characters (characters from a to Z, digits from 0-9, and the underscore _ character) "w"
    W Returns a match where the string DOES NOT contain any word characters "W"
     Returns a match if the specified characters are at the end of the string "Spain"

    Sets

    A set is a set of characters inside a pair of square brackets [] with a special meaning:

    SetDescription
    [arn] Returns a match where one of the specified characters (a, r, or n) are present
    [a-n] Returns a match for any lower case character, alphabetically between a and n
    [^arn] Returns a match for any character EXCEPT a, r, and n
    [0123] Returns a match where any of the specified digits (0, 1, 2, or 3) are present
    [0-9] Returns a match for any digit between 0 and 9
    [0-5][0=9] Returns a match for any two-digit numbers from 00 and 59
    [a-zA-Z] Returns a match for any character alphabetically between a and z, lower case OR upper case
    [+] In sets, +, *, ., |, (), $,{} has no special meaning, so [+] means: return a match for any + character in the string

    二 RE in python

    Python has a built-in package called re, which can be used to work with Regular Expressions.

    When you have imported the re module, you can start using regular expressions:

    FunctionDescription
    findall Returns a list containing all matches
    search Returns a Match object if there is a match anywhere in the string
    split Returns a list where the string has been split at each match
    sub Replaces one or many matches with a string

    The findall() Function

    The findall() function returns a list containing all matches.

    Example Print a list of all matches:

    import re

    txt = "The rain in Spain"
    x = re.findall("ai", txt)
    print(x)

    The list contains the matches in the order they are found.

    If no matches are found, an empty list is returned:

    import re

    txt = "The rain in Spain"
    x = re.findall("Portugal", txt)
    print(x)

    The search() Function

    The search() function searches the string for a match, and returns a Match object if there is a match.

    If there is more than one match, only the first occurrence of the match will be returned:

    Example Search for the first white-space character in the string:

    import re

    txt = "The rain in Spain"
    x = re.search("s", txt)

    print("The first white-space character is located in position:", x.start())

    If no matches are found, the value None is returned:

    import re

    txt = "The rain in Spain"
    x = re.search("Portugal", txt)
    print(x)

    The split() Function

    The split() function returns a list where the string has been split at each match:

    import re

    txt = "The rain in Spain"
    x = re.split("s", txt, 1)
    print(x)

    The sub() Function

    The sub() function replaces the matches with the text of your choice:

    import re

    txt = "The rain in Spain"
    x = re.sub("s", "9", txt,2)
    print(x)

    三 Python JSON module

    JSON is a syntax for storing and exchanging data.

    JSON is text, written with JavaScript object notation.

    Parse /pɑːz/解析 JSON

    Convert from JSON to Python

    If you have a JSON string, you can parse it by using the json.loads() method.

    The result will be a Python dictionary.

    Convert from Python to JSON

    If you have a Python object, you can convert it into a JSON string by using the json.dumps() method.

    PythonJSON
    dict Object
    list Array
    tuple Array
    str String
    int Number
    float Number
    True true
    False false
    None null

    四 Python time Module

    Python time.time()

    The time() function returns the number of seconds passed since epoch/ˈiːpɒk/新纪元 .

    For Unix system, January 1, 1970, 00:00:00 at UTC is epoch (the point where time begins).

    import time
    seconds = time.time()
    print("Seconds since epoch =", seconds)

    Python time.sleep()

    The sleep() function suspends (delays) execution of the current thread for the given number of seconds.

    import time

    print("This is printed immediately.")
    time.sleep(2.4)
    print("This is printed after 2.4 seconds.")

    Python time.localtime()

    The localtime() function takes the number of seconds passed since epoch as an argument and returns struct_time in local time.

    import time

    result = time.localtime(1545925769)
    print("result:", result)
    print(" year:", result.tm_year)
    print("tm_hour:", result.tm_hour)

    When you run the program, the output will be something like:

    result: time.struct_time(tm_year=2018, tm_mon=12, tm_mday=27, tm_hour=15, tm_min=49, tm_sec=29, tm_wday=3, tm_yday=361, tm_isdst=0)

    year: 2018
    tm_hour: 15

    Python time.gmtime()

    The gmtime() function takes the number of seconds passed since epoch as an argument and returns struct_time in UTC.

    import time

    result = time.gmtime(1545925769)
    print("result:", result)
    print(" year:", result.tm_year)
    print("tm_hour:", result.tm_hour)

    When you run the program, the output will be:

    result = time.struct_time(tm_year=2018, tm_mon=12, tm_mday=28, tm_hour=8, tm_min=44, tm_sec=4, tm_wday=4, tm_yday=362, tm_isdst=0)

    year = 2018
    tm_hour = 8

    Python datetime Module

    https://www.programiz.com/python-programming/datetime

  • 相关阅读:
    xBIM 基本的模型操作
    xBIM 应用与学习 (二)
    xBIM 应用与学习 (一)
    ABP WebApi 加载错误
    ABP 数据迁移
    AutoDesk Forge 获取令牌认证
    IIS 加载 JSON 错误 404 解决办法
    ASP.NET MVC 5 ABP DataTables (二)
    ASP.NET MVC 5 ABP DataTables (一)
    Windows10卡顿,磁盘 内存占用100%或比较多
  • 原文地址:https://www.cnblogs.com/fengshili666/p/14243326.html
Copyright © 2020-2023  润新知