模块時間、日時、ランダム、hashlib、要求

パッケージ

パッケージとは何ですか

モジュールパッケージの形式は、含まれている.pyファイルのフォルダを

なぜパッケージ

最初の少数の機能モジュールでは、次の拡張モジュール、モジュール名と使用状況が変化しないことをお勧めする必要がありますが、それはユーザーのために、と開発者のために便利ですが、多くの問題を管理するためのモジュールので、私は袋を使用モジュールの機能を拡張します。

1.はじめにパッケージ

  1. 基本的にモジュールは、実際に含まれている__init__.pyファイルフォルダを
  2. ガイドのパッケージがインポートすることです__init__.pyファイルを
  3. パッケージは、ファイルインポートモジュール、実行可能ファイルへのファイルパスにモジュール検索パスとして扱われなければならない方

2.絶対的および相対的インポートインポート

これは、パッケージだけで使用することができます

- 絶対的な輸入品

パッケージ名から。インポートモジュール名メソッド名

- 相対的な輸入品

  • 現在のファイルのインポートフォルダの代表が位置しています
  • ...最初のレベルに現在のファイルをインポートフォルダに配置されて表し
  • ...現在のレベルに代わってインポートフォルダファイルに

時間モジュール

タイムスタンプ

import time

print(time.time())   # 从1970年1月1日00:00:00开始计算到现在的秒数

時間のフォーマット

import time 
print(time.strftime('%Y-%m-%d %H:%M:%S'))  # 2019-09-28 17:15:47

print(time.strftime('%Y-%m-%d %X'))   # 2019-09-28 17:16:50

構造化された時間

import time
print(time.localtime())   # time.struct_time(tm_year=2019, tm_mon=9, tm_mday=28, tm_hour=17, tm_min=18, tm_sec=11, tm_wday=5, tm_yday=271, tm_isdst=0)
# 结构化基础时间
import time
print(time.localtime(0))   # time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=8, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=1, tm_isdst=0)

睡眠

import time 

start = time.time()
time.sleep(2)
end = time.time()
print(f'暂停了{end - start}秒')  # 暂停了2.000108003616333秒

datetimeモジュール

import datetime

# 输出当前时间
print(datetime.datetime.now())
# 2019-09-28 17:25:24.551237


# 加时间
now = datetime.datetime.now()
print(now + datetime.timedelta(days=3))
# 2019-10-01 17:28:24.710093


print(now.replace(year=1940))
# 1940-09-28 17:29:45.066855

randomモジュール

import random

# 0-1随机数
print(random.random())


# 1-100随机整数
print(random.randint(1,100))

# 1-3之间随机整数
print(random.randrange(1, 3))

# 打乱lt的顺序
lt = [1,2,4,60]
random.shuffle(lt)   # [4, 1, 60, 2]
print(lt)

# 随机选择lt中一个元素
print(random.choice(lt))

# random.seed
import random

random.seed(4)   # 给一个随机数种子
print(random.random())   # 只第一次随机生成,之后生成的数字就一样了
print(random.random())

# 如果不自定义种子,则种子按照当前的时间来

hashlibモジュール

ハッシュとは何ですか

ハッシュアルゴリズムである(のpython3。代わりに、MD5とSHAモジュールモジュール、メインプロバイダSHA1、SHA224、SHA256、SHA384、SHA512、MD5アルゴリズムを使用してのhashlibモジュールバージョン)は、アルゴリズムが動作を通しての束を取得し、着信コンテンツを受け付けハッシュ値。

import hashlib

m = hashlib.md5()
m.update(b'sayhello')   # 981fe96ed23ad8b9554cfeea38cd334a
print(m.hexdigest())   # 对于不同的字符而言,永不重复

ハッシュアルゴリズムのライブラリを破るヒット

pwd_list = [
    'hash3714',
    'hash1313',
    'hash94139413',
    'hash123456',
    '123456hash',
    'h123ash',
]

hash_pwd = '0562b36c3c5a3925dbe3c4d32a4f2ba2'

for pwd in pwd_list:
    m = hashlib.md5()
    m.update(pwd.encode('utf-8'))
    res = m.hexdigest()
    if res in hash_pwd:
        print(f'获取密码成功:{pwd}')  # 获取密码成功:hash123456

HMACモジュール

キー塩

import hmac

m = hmac.new(b'haha')
m.update(b'hash123456')
print(m.hexdigest())    # 24bb8daab11e526fc9b8178e51bc2ae7


m = m = hmac.new(b'sadness')
m.update(b'hash123456')
print(m.hexdigest())   # df405ffd019d6d3cd9a190fcab33aca5

要求モジュール

データをクロールするために使用することができる、ブラウザはアナログURLにリクエストを送信し、データを取得します

import requests

response = requests.get('https://www.baidu.com')
print(response.text)

モジュールの再

文字列は、特定の特性を満たす見つけるための文字列

使用は、実質的にモジュールを再:

import re  # 第一步,要引入re模块
a = re.findall("匹配规则", "这个字符串是否有匹配规则的字符")  # 第二步,调用模块函数
print(a)  # 以列表形式返回匹配到的字符串

^文字

始まる......

s = 'abcdabc'

res = re.findall('^abc',s)  
print(res)                  # ['abc']
res = re.findall('^bc',s)
print(res)                  # []

$文字

エンディング......

s = 'abcdabc'

res = re.findall('bc$',s)
print(res)             # ['bc']

:任意の文字

s = 'abc是dabc'

res = re.findall('abc.',s)
print(res)   # ['abc是']

\ D:デジタル

s = 'asdhg213214h4c'

res = re.findall('\d',s)
print(res)   # ['2', '1', '3', '2', '1', '4', '4']

\ D:非デジタル

s = 'asdhg2132 -14h4c'

res = re.findall('\D',s)
print(res)   # ['a', 's', 'd', 'h', 'g', ' ', '-', 'h', 'c']

\ W:非空で、数字、英字、アンダースコア

s = 'asdhg213214h4c'

res = re.findall('\w',s)
print(res)   # ['a', 's', 'd', 'h', 'g', '2', '1', '3', '2', '1', '4', 'h', '4', 'c']

\のW:空、数字を除いて、文字は、外部のアンダースコア

s = 'as;g:21?32    -14h4c\n'

res = re.findall('\W',s)
print(res)  # [';', ':', '?', ' ', ' ', ' ', ' ', '-', '\n']

\ S:空

s = 'asdhg2132 14h4c'

res = re.findall('\s',s)
print(res)    # [' ']

\ Sは:空ではありません

s = 'asdhg2132    -14h4c\n'

res = re.findall('\S',s)
print(res)  # ['a', 's', 'd', 'h', 'g', '2', '1', '3', '2', '-', '1', '4', 'h', '4', 'c']

+:文字の前に少なくとも1

s = 'abcdddd abcd abc ab'
print(re.findall('abc+', s))

?:文字0-1の前で一つ

s = 'abcdddd abcd abc ab a'
print(re.findall('abc?', s))  # ['abc', 'abc', 'abc', 'ab']

*少なくとも0の前の1つの文字

s = 'abcdddd abcd abc ab a'
print(re.findall('abcd*', s))   # ['abcdddd', 'abcd', 'abc']
s = 'abc bbc cbc dbc'
print(re.findall('[abc]bc', s))  # ['abc', 'bbc', 'cbc']

[^]:括弧内はできません

s = 'abc bbc cbc dbc'
print(re.findall('[^abc]bc', s))  # ['dbc']

|:または

s = 'abc bbc dbc'
print(re.findall('abc|bbc', s))  # ['abc', 'bbc']

{2}:直前の文字2

s = 'abcccab abcc'
print(re.findall('abc{2}', s))   # ['abcc', 'abcc'] 
print(re.findall('abc{0,2}', s))  # ['abcc', 'ab', 'abcc']

貪欲

(任意の文字)*(0-無限の数)

s = 'abcdefgaaaaaaaaaaag'
print(re.findall('a.*g',s))
# ['abcdefgaaaaaaaaaaag']

非貪欲モード

(任意の文字)*(0〜無限数)?

s = 'abcdefgbbbbbbbg'
print(re.findall('a.*?g',s))   # ['abcdefg']

学習:特別建設

# a(?=\d) :a后面是数字,但是不要数字,不消耗字符串内容
s = 'a123 aaaa a234 abc'
print(re.findall('a(?=\d)', s))  #['a', 'a']

print(re.findall('a(?=\w)', s))  #['a', 'a', 'a', 'a', 'a', 'a']

コンパイル

# 早期,re.findall不能传模式,只能用compile
s = '#@#@#@[email protected]$$$$////[email protected]$$#$#$[]][email protected]@$2423423lksdlfj#'
email_pattern = re.compile('\w+@\w+.com')
phone_patter = re.compile('\d{13}')
print(re.findall(email_pattern, s))

試合:最初から見てみると、エラーに見つけることができない、見つけるために探していません

s = 'abcd abcddd abc'
res = re.match('abcd*', s)
print(res.group())  # abcd

検索:見ていない、文字列から探して

s = 'abcd abcddd abc'
res = re.search('abcd*', s)
print(res.group())  # abcd

スプリットセグメンテーション

s = 'adad213114242wjdnadjia1241423daj'
print(re.split('\d+', s))  # ['adad', 'wjdnadjia', 'daj']

サブを交換してください

s = 'adad213114242wjdnadjia1241423daj'
print(re.sub('\d+', '  ', s))  # adad  wjdnadjia  daj

SUBNは、サブは何回も取り替え以上を置き換えます

s = 'adad213114242wjdnadjia1241423daj'
print(re.subn('\d+', '  ', s))   # ('adad  wjdnadjia  daj', 2)

補足:re.S

s = '''abc
abcabc*abc
'''

print(re.findall('abc.abc',s ))  # ['abc*abc']   原本.不匹配换行

print(re.findall('abc.abc',s ,re.S))  # ['abc\nabc', 'abc*abc']

グループ:カッコ限り

s = 'abc abcd abcddd'
print(re.findall('a(.)c(d)', s))  # [('b', 'd'), ('b', 'd')]

有名なグループ化

s = 'abc abcd abcddd'
print(re.search('a(?P<name1>.)c(?P<name2>d)', s).groupdict()) # {'name1': 'b', 'name2': 'd'}

超高度な使い方

s = 'abc123abc123'  # c123a
print(re.sub('c(\d+)a', ' ', s))
print(re.sub('c(?P<name1>\d+)a', ' \g<name1> ', s))  # \g<name1>这个东西不能替换掉

# ab bc123
# ab 123 bc123

おすすめ

転載: www.cnblogs.com/setcreed/p/11628032.html