我試圖只從一個串列(productUrl.txt)中過濾筆記本電腦條目,并將其保存在一個單獨的文本檔案(laptop.txt)中
。我的代碼:
ddic = ['Intel', 'AMD', 'Ryzen', 'lenovo', 'chuwi', '華為']
筆記本電腦 = []
file2 =open('./output/productUrl.txt'/span>,'r'/span>)
url = file2.readlines()
count = 0
for line in url:
count = 1
if any(keyword in url for keywordin ddic)
筆記本=網址
print(laptop, sep="
", file=open('./output/laptop.txt'/span>, 'a'/span>, encoding='utf-8')
file2.close
productUrl.txt包含像這樣的條目:
www.lazada.com.ph/products/chuwi-mijabook-intel-celeron-n3450-3k-display-i2194877054-s9863142699.html?search=1
www.lazada.com.ph/products/hot-selling-ovation-breeze-25-sataiii-120gb-240gb-480gb-ssd-i2320715371-s10522178610.html? search=1
www.lazada.com.ph/products/huawei-matebook-14-laptop-2k-fullview-display-amd-ryzen-4000-h-series-16gb-ram-512gb-ssd-i1262976645-s10442000469.html? search=1
www.lazada.com.ph/products/hot-selling-kingston-a2000-250gb-m2-2280-nvme-pcie-internal-ssd-solid-state-drive-sa2000m8250g-i2320553660-s10521592342.html?search=1
www.lazada.com.ph/products/lenovo-legion-5-series-r7000-r7000p-2021-brand-new-gaming-laptop-series-156-165hz-16gb-ram-512gb-ssd-i1889528550-s9658830606. html? search=1。
預期的結果是一個名為 laptop.txt 的文本檔案,其中包含 ff.
。www.lazada.com.ph/products/chuwi-mijabook-intel-celeron-n3450-3k-display-i2194877054-s9863142699.html?search=1
www.lazada.com.ph/products/huawei-matebook-14-laptop-2k-fullview-display-amd-ryzen-4000-h-series-16gb-ram-512gb-ssd-i1262976645-s10442000469.html? search=1
www.lazada.com.ph/products/lenovo-legion-5-series-r7000-r7000p-2021-brand-new-gaming-laptop-series-156-165hz-16gb-ram-512gb-ssd-i1889528550-s9658830606. html? search=1。
預先感謝你
uj5u.com熱心網友回復:
這里是我可能改變你的代碼的方法。你可以專注于for里面的if
for line in url。
if [brand for brand in ddic if brand in line] 。
laptop.append(line)
print(line, sep="
", file=open('./output/laptop.txt'/span>, 'a'/span>, encoding='utf-8')
file2.close
uj5u.com熱心網友回復:
ddic = ['Intel'/span>, 'AMD'/span>, 'Ryzen', 'lenovo', 'chuwi', '華為']
筆記本電腦 = []
file2 =open('./output/productUrl.txt'/span>,'r'/span>).read()
data=file2.split("
")
laptop=[i for i in data for j in ddic if j in i ]
print(筆記本電腦)
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/312049.html
標籤:
上一篇:用自定義匹配函式對2個資料框架的所有行進行比較的For回圈
下一篇:卸載抖音和微博的一天……
