第一部分 编程基础
@表达式
**
%
//
@
>>> int(3.4) 3
>>>round(3.555,2)
3.56
@判断条件时:0和0.0和‘’都是False
@终止进程
sys.exit()
@
>>> m=[1,2,3]
>>> print(m.pop())
3
>>> print(m)
[1, 2]
@陷入无限循环时
ctrl+c
@
print('cats','dog',sep=',')#Seperate
cats,dog
@try…except中,try里面有多个的话,一旦有触发except的,就不会再继续try。
@多重赋值
cat=['fat','black','loud']
size,color,disposition=cat
@sort()使用ASCII码排序,排序大写在小写前面
>>>spam=['a','Z','b','z']
>>>spam.sort(key=str.lower)#lower后面没有括号
>>>spam
['a', 'b', 'Z', 'z']
@字典中的get和setdefault
items={'apple':3,'cup':2}
print("i get {0} apples and {1} eggs".format(items.get('apple',0),items.get('egg',3)))
print(items)
print('---')
print("i get {} apples and {} eggs".format(items.setdefault('apple',0),items.setdefault('egg',3)))
print(items)
@迭代字符串,数各字母数
>>> message='lalalalawoshimaibaodexiaohangjia' >>> count={} >>> for character in message: count.setdefault(character,0) count[character]=count[character]+1 >>> print(count)
@漂亮打印
import pprint
>>> pprint.pprint(count)
@方法
index()
insert()
remove() tuple()
str()
list()
@转义字符:反斜杠
@is字符串方法
isalpha()
isalnum()
isdecimal()
isspace()
只有字母;
只有字母和数字;
只有数字;
只有空格、制表符、换行符。
@其它字符串方法
startswith()
endswith() join()
split() center(20,'=')
strip('abc')
@监控鼠标和键盘
https://www.jb51.net/article/146800.htm
调用剪切板(自制密码保管器)
import pyperclip,sys
pw={...}
count=sys.argv[1]
pyperclip.copy(pw[count])#保存到剪切板,直接鼠标右键粘贴就行
print(pyperclip.paste())#打印剪切板里的内容
第二部分 自动化任务
@正则
re.compile()==>search==》match对象==》group()#仅匹配一次
re.compile(()())==...》groups()
()?
(){}?
.*? #是满足条件的情况只匹配一次,即最小匹配.
*
+
|
.#除换行外通配,包括汉字;compile里面加re.DOTALL,则匹配所有字符
re.I
re.VERBOSE
[]内的普通正则符号不会被解释;
@读写文件
os
os.path.join(a,b)
os.chdir(a)
os.getcwd()
os.makedirs(a)
当前目录的相对路径‘.\’
os.path.abspath('.')==os.getcwd() path=r'E:\新python资料20180629\基础及爬虫入门视频'
os.path.basename(path)
>>>'基础及爬虫入门视频'
os.path.dirname(path)
>>>'E:\\新python资料20180629' os.path.getsize(path)
>>>4096
os.listdir(path)#本层的文件及文件夹名
>>>['01-Python基础-第01天(Linux基本操作)', '02-Python基础-第02天(Linux基本操作)',...] os.path.exists(path)
永久删除文件
os.unlink(path)
os.rmdir(dir)#必须是空文件夹
shutil.rmtree(dir)
删到垃圾箱
send2trash(path)
查看所有文件夹和文件名
import os
for flodername,subfolders,filenames in os.walk(path):
print(flodername)
for subfloder in subfo.ders:
print('{}:{}'.format(flodername,subfloder)
for filename in filenames:
print('{}:{}'.format(flodername,filename)
@zipfile模块
@批量调整文件名
"""批量调整名字"""
import re,os,shutil
#正则表达式
re_or=re.compile(r'(\d+)-(\d+)-(\d+)(.*)')
#检索文件夹内所有名字并匹配,组成新名字
path='' for or_name in os.listdir(path):
day_name=re_or.search(or_name).group(1)
mon_name=re_or.search(or_name).group(2)
year_name=re_or.search(or_name).group(3)
last_name=re_or.search(or_name).group(4)
new_name=mon_name+day_name+year_name+last_name
#***关于group的位置,compile(r'(1)(2(3))(4)')
or_file_path=os.path.join(path+or_name)
new_file_path=os.path.join(path+new_name)
shutil.move(or_file_path,new_file_path)
@项目:将一个文件夹备份到zip文件,P168
@第十章未看
@webbrowser,p192
@selenium,p210及以后
@