I'm trying to filter laptop entries only from a list (productUrl.txt) and save it in a seperate text file (laptop.txt)
My code:
ddic = ['Intel', 'AMD', 'Ryzen', 'lenovo', 'chuwi', 'huawei']
laptop = []
file2 =open('./output/productUrl.txt','r')
url = file2.readlines()
count = 0
for line in url:
count = 1
if any(keyword in url for keyword in ddic):
laptop = url
print(laptop, sep="\n", file=open('./output/laptop.txt', 'a', encoding='utf-8'))
file2.close
The productUrl.txt contains entries like this:
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
the expected result is a text file called laptop.txt containing the 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
Thank you in advance
CodePudding user response:
Here is how I may change your code. You can focus on the if
inside for
for line in url:
if [brand for brand in ddic if brand in line]:
laptop.append(line)
print(line, sep="\n", file=open('./output/laptop.txt', 'a', encoding='utf-8'))
file2.close
CodePudding user response:
ddic = ['Intel', 'AMD', 'Ryzen', 'lenovo', 'chuwi', 'huawei']
laptop = []
file2 =open('./output/productUrl.txt','r').read()
data=file2.split("\n")
laptop=[i for i in data for j in ddic if j in i]
print(laptop)