python 里的split用法


        with open('IP.txt', 'r', encoding="utf-8") as f:#打開IP的地址 讀取
            txt = f.read()
            return random.choice(txt.split('\n')) #反饋合成列表 隨即選擇一個''
例如: IP.txt 里的內容 隨即提取每一行
182.92.242.11:80
58.52.201.117:8080
175.43.156.60:9999
47.110.59.21:8080
175.43.34.49:9999
1.198.72.164:9999
222.190.110.167:808
使用txt.split('\n') 后成這樣
['182.92.242.11:80', '58.52.201.117:8080', '175.43.156.60:9999', '47.110.59.21:8080', '175.43.34.49:9999', '1.198.72.164:9999', '222.190.110.167:808']
'''

 接着用法:

url = 'http://www.dmoz.org/Computers/Programming/Languages/Python/Books/'
filename = url.split("/")[-2]
print(filename)

'''
Books     

以/為分界線把url分割成列表,-2取倒數第二個,分割網站 提取名字很好用。
'''

 


免責聲明!

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



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