命令
- python:
cmd=>python
import this
- 退出:quit()
- pip install {name}
- 查看關鍵字
import keyword
keyword.kwlist
lambda語法
形式: lambda argument_list: expression
其中,lambda是Python預留的關鍵字,argument_list和expression由用戶自定義。具體介紹如下。
- 這裏的argument_list是參數列表,例如:
a, b
a=1, b=2
*args
**kwargs
a, b=1, *args
空
… - expression是一個關於參數的表達式。表達式中出現的參數需要在argument_list中有定義,並且表達式只能是單行的。例如:
1
None
a + b
sum(a)
1 if a >10 else 0
…
部分Python內置函數接收函數作爲參數。典型的此類內置函數有這些。
filter函數。用於指定過濾列表元素的條件。例如filter(lambda x: x % 3 == 0, [1, 2, 3])指定將列表[1,2,3]中能夠被3整除的元素過濾出來,其結果是[3]。
sorted函數。用於指定對列表中所有元素進行排序的準則。例如sorted([1, 2, 3, 4, 5, 6, 7, 8, 9], key=lambda x: abs(5-x))將列表[1, 2, 3, 4, 5, 6, 7, 8, 9]按照元素與5距離從小到大進行排序,其結果是[5, 4, 6, 3, 7, 2, 8, 1, 9]。
map函數。用於指定對列表中每一個元素的共同操作。例如map(lambda x: x+1, [1, 2,3])將列表[1, 2, 3]中的元素分別加1,其結果[2, 3, 4]。
reduce函數。用於指定列表中兩兩相鄰元素的結合條件。例如reduce(lambda a, b: ‘{}, {}’.format(a, b), [1, 2, 3, 4, 5, 6, 7, 8, 9])將列表 [1, 2, 3, 4, 5, 6, 7, 8, 9]中的元素從左往右兩兩以逗號分隔的字符的形式依次結合起來,其結果是’1, 2, 3, 4, 5, 6, 7, 8, 9’。
格式化輸出
'%s' %title
post請求
https://www.cnblogs.com/yourstars/p/8196054.html
下載圖片
urllib.request.urlretrieve
import os,stat
import urllib.request
img_url="*"
file_path_1="D:/"
file_name="test"
try:
if not os.path.exists(file_path_1):
os.makedirs(file_path_1)
#get pic suffix
file_suffix=os.path.splitext(img_url)[1]
newFilename = '{}{}{}{}'.format(file_path_1, os.sep, file_name, file_suffix)
#download image
urllib.request.urlretrieve(img_url,filename=newFilename)
except IOError as e:
print(e)
except Exception as e:
print(e)
raw_input
1、在 Python2.x 中 raw_input( ) 和 input( ),兩個函數都存在,其中區別爲:
raw_input( ) 將所有輸入作爲字符串看待,返回字符串類型。
input( ) 只能接收“數字”的輸入,在對待純數字輸入時具有自己的特性,它返回所輸入的數字的類型( int, float )。
2、在 Python3.x 中 raw_input( ) 和 input( ) 進行了整合,去除了 raw_input( ),僅保留了 input( ) 函數,其接收任意任性輸入,將所有輸入默認爲字符串處理,並返回字符串類型。
isinstance函數
判斷一個對象是否是一個已知的類型。
isinstance(object, classinfo)
>>>a = 2
>>> isinstance (a,int)
True
>>> isinstance (a,str)
False
>>> isinstance (a,(str,int,list)) # 是元組中的一個返回 True
True
isinstance() 與 type() 區別:
type() 不會認爲子類是一種父類類型,不考慮繼承關係。
isinstance() 會認爲子類是一種父類類型,考慮繼承關係。
如果要判斷兩個類型是否相同推薦使用 isinstance()。
數字轉換爲字符串
第一種是str(),將值轉換爲用戶便於閱讀的形式;
另一種是repr(),將值轉換爲合法的python表達式。
>>> print repr("Hello, world!")
'Hello, world!'
>>> print repr(10000L)
10000L
>>> print str("Hello, world!")
Hello, world!
>>> print str(10000L)
10000
自增運算
python沒有++
i+=1
快捷鍵
刪除當前行:ctrl+shift+k
解析excel
https://www.cnblogs.com/zhang-jun-jie/p/9273721.html
#遍歷
nrows = sheet2.nrows
for i in range(nrows):
print(sheet2.row_values(i))
數字1.0問題
if ctype == 2 and cell % 1 == 0.0: # ctype爲2且爲浮點
cell = int(cell) # 浮點轉成整型
字符串去空格
.strip()
.lstrip()
.rstrip()
字符串判空
if s.strip()=='':
print 's is null'
使用字符串長度判斷
len(s) ==0 則字符串爲空
isspace判斷是否字符串全部是空格
s.isspace() == True
>>> somestr=''
>>> sometuple=()
>>> somelist=[]
>>> somedict={}
>>> not somestr
True
>>> not sometuple
True
>>> not somelist
True
>>> not somedict
True
range函數
range(start, stop[, step])
python range() 函數可創建一個整數列表,一般用在 for 循環中。
start: 計數從 start 開始。默認是從 0 開始。例如range(5)等價於range(0, 5);
stop: 計數到 stop 結束,但不包括 stop。例如:range(0, 5) 是[0, 1, 2, 3, 4]沒有5
step:步長,默認爲1。例如:range(0, 5) 等價於 range(0, 5, 1)
# 導入模塊
from selenium import webdriver
driver=webdriver.***()
url=""
driver.get(url)
# cookies-dict
cookie_list=driver.get_cookies()
# 遍歷cookie
for cookie in cookie_list:
print(cookie['name'])
print(cookie['value'])
cookie_dict[cookie['name']]=cookie['value']
print(cookie_dict)
https://blog.csdn.net/t8116189520/article/details/80319339
- https://blog.csdn.net/zjuxsl/article/details/79437563
- https://blog.csdn.net/pan_yt/article/details/79050961
- https://www.runoob.com/python/python-func-raw_input.html
- https://segmentfault.com/a/1190000016674171?utm_source=tag-newest
- https://www.jb51.net/article/155192.htm
- https://www.cnblogs.com/wanglei-xiaoshitou1/p/9401261.html
- https://www.runoob.com/python/python-func-range.html