Jamie's Blog

Talk is cheap, show me the code.

Python 中的正则表达式

Regex in Python

在编写处理字符串的程序或网页时,经常会有查找符合某些复杂规则的字符串的需要。正则表达式就是用于描述这些规则的工具。换句话说,正则表达式就是记录文本规则的代码。

字符

. 匹配除换行符之外的任意字符,当re.S(DOTALL)标记被指定时,这可以匹配包括换 行符的任意字符

\d 可以匹配一个数字 等同于[0-9]

\D 等同于[^0-9]匹配非数字

\w 等同于[a-z0-9A-Z_]匹配大小写字母、数字和下划线

\W 等同于[^a-z0-9A-Z_]等同于上一条取非

A|B 可以匹配A或B,所以(P|p)ython可以匹配’Python’或者’python’

^ 表示行的开头,^\d表示必须以数字开头

\$ 表示行的结束,\d$表示必须以数字结束

\s 可以匹配空白字符,等价于 [\t\n\r\f]。(也包括Tab等空白符)

\S 匹配任意非空字符

\n 匹配一个换行符

\t 匹配一个制表符

\A 匹配字符串开头

\Z 匹配字符串结尾,如果存在换行,只匹配到换行前的结束字符串

\z 匹配字符串结尾,如果存在换行,同时还会匹配换行符

\G 匹配最后匹配完成的位置 (不懂需要理解)

What does named tuple do?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
student=('Jim','16','male','Jim123@qq.com')

# name
print(student[0])

# age
print(student[1])

# sex
print(student[2])

#email
print(student[3])

#用元祖的下标索引来访问元组中的值,0,1,2,3名字没有意义
Jim
16
male
Jim123@qq.com

is and ==

python中的比较、相等性

1
2
3
4
5
L1 = [1,2,['A','B']]
L2 = [1,2,['A','B']]
L3 = L1
print(L1 == L2, L1 is L2)
print(L1 == L3, L1 is L3)
True False
True True

How to find common keys in dict

字典生产式

1
2
3
4
5
6
7
8
from random import randint,sample 
# random.sample(sequence, k),从指定序列中随机获取指定长度的片断。sample函数不会修改原有序列。
s1 = {x: randint(3,4) for x in sample('abcdef',randint(4,6))}
s2 = {x: randint(3,4) for x in sample('abcdef',randint(4,6))}
s3 = {x: randint(3,4) for x in sample('abcdef',randint(4,6))}
print(s1)
print(s2)
print(s3)
{'d': 4, 'a': 3, 'e': 3, 'f': 4}
{'c': 4, 'b': 3, 'e': 3, 'f': 4}
{'e': 3, 'c': 4, 'd': 3, 'a': 3}

How to Scrape All ETH Addresses from A Certain Telegram Chat

First attempt: Telethon (doesn’t work)

Telegram is a popular messaging application. This library is meant to make it easy for you to write Python programs that can interact with Telegram.

1
2
3
4
5
6
7
8
9
10
11
12
13
#Creating a client

from telethon import TelegramClient
import socks

api_id = 240869
api_hash= '2e5e8cb9cda41113da881a986271029d'

client = TelegramClient('sqlite3',api_id,api_hash,proxy=(socks.SOCKS5,'localhost',1080))
client.connect()
phone_number = '8613480975957'
client.send_code_request(phone_number)
myself = client.sign_in(phone_number,input('Enter code:'))
Enter code:5603960942

Proudly powered by Hexo and Theme by Hacker
© 2019 Jamie