《python編程快速上手》


第一部分 編程基礎

@表達式

**
%
//

@

>>> int(3.4)

3

>>>round(3.555,2)
3.56

@判斷條件時:00.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

@tryexcept中,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及以后

@


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM