import codecs
with codecs.open("aaa.txt", "w") as f:
f.writelines("this\nis\na\ntest\nfile!!")
dict1 = dict(a=1, b=2)
print(sorted(dict1.items(), key=lambda d:d[1]))
输出://以列表的方式输出
[('a', 1), ('b', 2)]
如果需要以字典形式的方式输出,则需要强制转换:
dict1 = dict(a=1, b=2)
print(dict(sorted(dict1.items(), key=lambda d:d[1])))
a = lambda x:x+2
print(a(2))
aa =[x for x in range(1, 10) if x%2 == 0]
print(aa)
输出:
[2, 4, 6, 8]
return:直接返回结果,不再执行下面的语句。
yield:直接返回结果,再次执行会从yield下面的语句开始执行。
dict1={1:[1,11,111],2:[2,22,222]}
dict2={3:[3,33,333],4:[4,44,444]}
aaa = dict1.copy()
aaa.update(dict2)
print(aaa)
在不改变原有函数的情况下, 在函数的前面或者后面分别增加一些功能。
def startEnd(fun):
def wrap(name):
print("Start")
fun(name)
print("End")
return wrap
@startEnd
def hello(name):
print("hello {0}".format(name))
if __name__ == '__main__':
hello("zhdya")
输出:
Start
hello zhdya
End
判断文件或者目录是否存在:
os.path.exists("qqq")
返回值:True 或者 False
import os
print(os.path.exists("qqq"))
输出:
False
执行系统命令:
os.system("ipconfig")
得到系统命令的结果:
result = os.popen("ipconfig").read()
print(result
函数 | 描述 |
---|---|
json.dumps() | 对数据进行编码,将 Python 对象编码成 JSON 字符串。 |
json.loads() | 对数据进行解码,将已编码的 JSON 字符串解码为 Python 对象。 |
json.dump() | 把python对象 写入文件 |
json.load() | 把文件-》 python对象 |
import sys
获取第一个参数:
sys.argv[1]
import random
获取一个0-1之间的随机小数
print(random.random())
随机1-10之间的整数
print(random.randint(1, 10))
从列表中随机取出3位数
list1 = [1, 3, 5, 7, 9]
print(random.sample(list1, 3))
从1-100取出所有的奇数
print(random.randrange(1, 100, 3))
import string
print(string.ascii_letters) ##打印所有的大小写字母
print(string.digits) ##打印所有的数字
print(string.ascii_lowercase) ##打印所有的小写字母
print(string.ascii_uppercase) ##打印所有的大写字母
print(string.hexdigits) ##打印0-9以及a-f的大小写字母
print(string.printable) ##打印所有的字符+数字+符号+特殊符号
import logging
logging.basicConfig(level=logging.DEBUG) ##更改警告级别
logger = logging.getLogger(__name__) ##常用的一种方式
logger.debug("this is a debug info.") ##debug模式(默认级别不打印)
logger.info("this is a info info.") ##info模式(默认级别不打印)
logger.warning("this is a warning info.") ##warning模式
logger.error("this is a error info.") ##error模式
logger.critical("this is a critical info.") ##critical模式
输出:
DEBUG:__main__:this is a debug info.
INFO:__main__:this is a info info.
WARNING:__main__:this is a warning info.
ERROR:__main__:this is a error info.
CRITICAL:__main__:this is a critical info.
INFO:__main__:this is a debug info.
import hashlib
mm = hashlib.md5()
src = "thisiszhdya"
mm.update(src.encode("utf-8"))
print(mm.hexdigest())
输出:
d1852a489f822553f3f4059615b8bbf4
from _datetime import datetime
print(datetime.now()) ##打印现在的时间
print(datetime.now().strftime("%Y-%m-%d")) ##以特定的格式打印
import time ##导入time模块
time.sleep(5) ##暂停5秒
StringIO && ByteIO
from io import StringIO,BytesIO
s = StringIO()
s.write("hello\nworld!!")
print(s.getvalue()) ##获取写入内存的值
s.truncate() ##清空内存中的值
正则表达式是一个特殊的字符序列,它能帮助你方便的检查一个字符串是否与某种模式匹配。
re模块是python中处理正则表达式的一个模块,通过re模块的方法,把正则表达式pattern编译成正则对象,以便使用正则对象的方法。
小实例:
匹配如下:
<h1>xxx</h1>
分析:
因为比较特殊,所以选择分组匹配:
<(?P<tagname>\w*)>.*</(?P=tagname)>
<> ##首先匹配中括号
(?P<name>正则) ##是一个固定的模式
\w+ ##一个或多个单词字符
.* ##匹配中间任意
<> ##再次匹配
</(?P=tagname)> ##首尾相对应即可
re.match 尝试从字符串的起始位置匹配一个模式,如果不是起始位置匹配成功的话,match()就返回none。
re.match(pattern, string, flags=0)
函数参数说明:
参数 | 描述 |
---|---|
pattern | 匹配的正则表达式 |
string | 要匹配的字符串。 |
flags | 标志位,用于控制正则表达式的匹配方式,如:是否区分大小写,多行匹配等等。 |
匹配成功re.match方法返回一个匹配的对象,否则返回None。
实例
#!/usr/bin/python3
import re
s = "<h1>xxx</h1>"
reg = re.compile(r"<(?P<tagname>\w*)>.*</(?P=tagname)>")
print(reg.match(s).group())
输出:
<h1>xxx</h1>
匹配规则:
abc<h1>xxxxx</h1>
match的规则是从头开始匹配,一旦从a开始发现不是的,就会直接返回None值;
search的规则是从头开始匹配,一旦发现a不是的自动从b开始,b也不是就从c开始,直至找到符合筛选条件的内容;
import re
s = "abc<h1>xxx</h1>"
reg = re.compile(r"<(?P<tagname>\w*)>.*</(?P=tagname)>") ## r 代表着对特殊符号转义
print(reg.match(s))
print(reg.search(s).group())
输出:
None
<h1>xxx</h1>
通过如上的match和search比对:
Match的效率是最高的,就要求我们正则表达式要写正确!
使用率是最高的,因为前面所讲到的match和search均只匹配一次,一旦有多次,就需要使用findall了。
import re
s = "1one2two3three4four"
reg = re.compile(r"\d")
print(reg.findall(s))
输出: ##返回值均为列表
['1', '2', '3', '4']
以正则表达式为分隔符,筛选:
import re
s = "1one2two3three4four"
reg = re.compile(r"\d")
print(reg.split(s))
输出: ##返回值均为列表
['', 'one', 'two', 'three', 'four']
假如我们需要取到列表中的某个值,我们需要修改如下即可:
import re
s = "1one2two3three4four"
reg = re.compile(r"\d")
print(reg.split(s)[3])
输出:
three