python學習 模組

2021-09-12 21:20:04 字數 2415 閱讀 4483

time

import time

time.ctime()獲取當前時間的字串

time.localtime() 返回值是strcut_time型別的物件

st = time.localtime()

st.tm_yday 現在是今年第幾天

st.tm_hour 現在是今天第幾個小時

st.tm_mon 現在是今年的第幾個月

time.strftime() 將struct_time型別物件格式化為字串

time.strftime(『%y-%m-%d %h:%m:%s』,st)

time.sleep() 強制掛起當前程序

requests

import requests

r = requests.get(『url』)

r.status_code

r.headers[『content-type』]

r.text

r.json() 將返回的json資料轉化為乙個python字典

base64

編碼和解碼

import base64

base64.b64encode(b』hello,shiyanlou!』)

base64.b64decode(b』』)

copy

淺複製 深複製

copy.copy copy.deepcopy

l2 = copy.copy(l)

l2 = copy.deepcopy(l)

configparser

編輯配置檔案

from configparser import configparser

config = configparser()

config.read(『file』,encoding=』utf-8』)

config.sections() 獲取所有的配置組

config.options(『section1』) 獲取sections1配置組的所有options

config.items(『section1』) 獲取section1下的所有鍵值對

config.get(『sections1』,』item1』) 獲取section1 配置組下的item1對應的值

config.add_section(『section2』) 新增新的section2配置組

config.set(『section2』,』option1』,』123』) 對section2新增新的option

config[『section2』][『option1』]=』456』 修改option1的值為456

with open(『file』,』w』,encoding=』utf-8』) as file1:

config.write(file1) 將修改後的配置寫入配置檔案中

collections

提供一些特別的容器

from collections import iterable 判斷是否是可迭代物件

isinstance(『shiyanlou』,iterable) 字串是可迭代物件

isinstance(123,iterable) 數值不是可迭代物件

from collections import iterator 判斷是否是迭代器

from collections import generator 判斷是否是生成器

from collections import namedtuple

from collections import counter

c = counter(『wwww12345』)

c.most_common(3)

re正規表示式

findall 獲取全部能夠匹配的片段,放到乙個列表裡,第乙個引數為正規表示式,第二個引數為被匹配物件

\d 0-9數字

\d 匹配所有非數字

\w 匹配所有單詞字元,包括大小寫字母、數字、下劃線、中文

\w 匹配剩下的,空格、換行符、特殊字元等

字符集 匹配任意符合條件的字元

re.findall(『char[ad]』,s)

re.findall(『char[^ad]』,s) ^代表非

re.findall(『char_[a-c]』,s) a-c表示a至c

空白字元 \s 『 』空格、\n 換行符、\t 製表符、\r回車符

re.finall(『\s』,s)

{} 標定匹配字元的數量,預設匹配模式是貪婪模式,即盡可能多的匹配字元

?表示非貪婪模式,選取最少的匹配字元

re.findall(『a-z』,s)

*匹配任意數量的字元

?匹配0或1個字元

+匹配1或多個字元

.匹配除換行符\n以外任意1個字元

邊界字元 ^行的開始處匹配 $行的末尾處進行匹配

字元組 () 為匹配成功的字串進行分組

python學習(模組)

模組搜尋路徑 匯入乙個叫 spam 的模組時,直譯器先在當前目錄中搜尋名為 spam.py 的檔案,然後在環境變數 pythonpath 指琮的目錄列表中搜尋,然後是環境變數 path 中的路徑列表。如果 pythonpath 沒有設定,或者檔案沒有找到,接下來搜尋安裝目錄,在 unix 中,通常是...

python 模組學習

一 from django.contrib.auth.hashers import make password 通過函式名即可發現,主要有兩個函式,分別是建立密碼和驗證 用法ps 123456 dj ps make password ps,none,pbkdf2 sha256 建立django密碼,...

python學習 模組

一.模組包括三種 應用程式自定義模組 1.建立兩個py檔案,乙個是定義函式用的cal.py 注 如果你對python感興趣,我這有個學習python基地,裡面有很多學習資料,感興趣的 q群 688244617 def add x,y return x y另檔案test.py乙個呼叫cal.py的函式...