【線性動態規劃】最長不下降子序列(LIS)
系列文章目錄
今天遇到的新的python語法
文章目錄
一、os模組
作者:python小達
連結:https://www.zhihu.com/question/322177702/answer/1662279000
來源:知乎
os模組:使用環境64位的fedora 18,python版本是python 2.7.31、os.getcwd()函式 功能:獲取當前目錄,python 的工作目import os
pwd = os.getcwd()
print (pwd)2、os.name 函式 功能:獲取當前使用的作業系統(獲取資訊不夠詳細) 其中 ‘nt’ 是 windows,‘posix’ 是 linux 或者 uniximport os
name = os.name
if name == ‘posix’:
print (“this is Linux or Unix”)
elif name == ‘nt’:
print (“this is windows”)
else:
print (“this is other system”) 3、os.remove()函式 功能:刪除指定檔案 eg:刪除 file.txt 檔案import os
os.remove(’file.txt‘)4、os.removedirs()函式 功能:刪除指定目錄 eg:刪除 file目錄import os
os.removedirs(‘file’)5、os.system()函式 功能:執行shell命令 eg:執行ls -a > 1.txt命令import os
os.system(‘ls -a > 1.txt’)6、os.mkdir()函式 功能:建立一個新目錄 eg:建立一個 file 目錄import os
os.mkdir(‘file’)7、os.chdir()函式 功能:改變當前路徑到指定路徑 eg:我現在從當前路徑到 filepath 所指定的路徑下import os
filepath = ‘/home’
pwd = os.getcwd()
print (pwd)
os.chdir(filepath)
pwd = os.getcwd()
print (pwd)8、os.listdir()函式 功能:返回指定目錄下的所有目錄和檔案 eg:列出當前目錄下的所有檔案和目錄import os
pwd = os.getcwd()
name = os.listdir(pwd)
for filename in name:
print (filename)
二、lxml
https://blog.csdn.net/qq_35208583/article/details/89041912
我的理解是 xpath 就是將網頁樹化,然後通過枝葉找到我們所需要的東西而不是爬取整個網頁。
三、split函式(分割函式)
原文網址https://blog.csdn.net/u014125025/article/details/100639792
Python中split():split() 方法可以實現將一個字串按照指定的分隔符切分成多個子串,這些子串會被儲存到列表中(不包含分隔符),作為方法的返回值反饋回來。
s.split(sep,maxsplit)
str:表示要進行分割的字串;
sep:用於指定分隔符,可以包含多個字元。此引數預設為 None,表示所有空字元,包括空格、換行符“\n”、製表符“\t”等。不含引數,以空格進行分割,含引數時,以該引數進行分割。
maxsplit:可選引數,用於指定分割的次數,最後列表中子串的個數最多為 maxsplit+1。如果不指定或者指定為 -1,則表示分割次數沒有限制。
1.情況一
s=‘an apple a day’
def split(s):
return s.split(‘a’)
print(s.split())
上述程式碼執行結果
2.情況二,不帶引數,以空格分隔
s=‘an apple a day’
def split(s):
return s.split(’’)
print(s.split())
3.情況三
s=‘an apple||a day’
def split(s):
return s.split(’||’)
print(s.split())
4.情況四
s=‘an apple || a day’
def split(s):
return s.split(’||’)
print(s.split())
5.情況5
s=‘anppappleppappday’
print(s.split(‘p’))
6.情況6,類似於切豆角,一個字元P,切一刀(分隔一次),10個字元P,分隔10次,有10個逗號(刀印)。
s=‘ppanppappleppappday’
print(s.split(‘p’))
s=‘an.apple.a.day’
print(s.split(’.’,2))
8.分隔2次,並選第二個片段
s=‘an.apple.a.day’
print(s.split(’.’,2)[1])
總結
有些具體的細節還是沒搞太清楚,在實踐裡去搞懂吧,把今天學的爬蟲程式碼學好,其他的在其他專案裡再去學習