Reputation: 159
It has been a long night and day. I could use some help why this is not working can not think straight. This script opens a .csv file checks if a certain word is contained in a the title which is at row[4] and then it will loop through a dictionary that contains list and if the word is in the title then it will return the dictionary key to be written to a new csv file.
What I am getting is uncategorized for every row which should not be correct because the title does contain words in the list.
Thanks for the help in advance.
Example CSV in BrandsMart USA,http://www.BrandsMartUSA.com,BrandsMart Product Catalog,01:30.8,Mini Bass King Jr Bluetooth Portable Outdoor Speaker,2Boom BX320K,2Boom BX320K Mini Bass King Jr Bluetooth Portable Outdoor Speaker,2BOOMBX320K,2Boom,BX320K,7.08192E+11,,USD,,19.88,19.88,,http://www.example.com,http://www.ftjcfx.com/image-8299186-11133500,https://www.brandsmartusa.com/images/product/large/20225377.jpg,Bluetooth & Docking Speakers,,,,,,,,,,,,,,,,yes,,, BrandsMart USA,http://www.BrandsMartUSA.com,BrandsMart Product Catalog,01:30.8,Mini Bass King Jr Bluetooth Portable Outdoor Speaker,2Boom BX320R,2Boom BX320R Mini Bass King Jr Bluetooth Portable Outdoor Speaker,2BOOMBX320R,2Boom,BX320R,7.08192E+11,,USD,,19.88,19.88,,http://www.example.com,http://www.ftjcfx.com/image-8299186-11133500,https://www.brandsmartusa.com/images/product/large/20225393.jpg,Bluetooth & Docking Speakers,,,,,,,,,,,,,,,,yes,,, BrandsMart USA,http://www.BrandsMartUSA.com,BrandsMart Product Catalog,01:30.8,Bluetooth Noise-Cancelling Earbud Headphones,2Boom EPBT690B,2Boom EPBT690B Bluetooth Noise-Cancelling Earbud Headphones,2BOOMEPBT690B,2Boom,EPBT690B,7.44751E+11,,USD,,9.88,9.88,,http://www.example.com,http://www.ftjcfx.com/image-8299186-11133500,https://www.brandsmartusa.com/images/product/large/20225398.jpg,Earbuds,,,,,,,,,,,,,,,,yes,,,
Using Python 3
import os, csv, time
csv_path = os.path.dirname(os.path.abspath(__file__))
row_list = []
# Appliances
category_list = {"Appliance Accessories": ['Air Conditioner Accessories', 'Air Purifier Accessories', "Coffee Maker Accessories",
'Dishwasher Accessories', 'Food Processor Accessories', 'Heater Accessories',
'Humidifier Accessories', 'Humidifier Accessories', 'Mixer Accessories',
'Range & Oven Accessories', 'Range Hood Accessories', 'Refrigerator Accessories',
'Vacuum Accessories', 'Washer & Dryer Accessories'],
"Electronics Accessories": ['cables & adapters', 'audio accessories', 'video accessories', 'camcorder accessories',
'cell phone accessories', 'clock radios', 'Digital Book Reader Accessories',
'Digital Picture Frames', 'Electronics Cases & Bags', 'GPS Accessories',
'Projector Accessories', 'Telephone Accessories', 'batteries', 'battery'],
"Photography": ['Camcorders', 'Cameras', 'Digital Camera Accessories', 'Digital Cameras', 'Camera', 'Digital Camera',
'Photography', 'Darkroom']}
category = ""
with open(csv_path + "/pre.csv") as f:
reader = csv.reader(f)
for row in reader:
for k, val in category_list.items():
for v in val:
if v.lower() in row[4].lower():
category = k
else:
category = "Uncategorized"
new_row = [str(row[0]), # company
str(row[1]), # company url
str(row[4]), # product name
str(row[5]), # keywords
str(row[6]), # descripition
str(row[7]), # sku
str(row[8]), # manufacturer
str(row[13]), # saleprice
str(row[14]), # price
str(row[15]), # retailprice
str(row[17]), # buy_link
str(row[19]), # product_image_url
str(row[31]), # promotional_text
str(row[36]), # stock
str(row[37]), # condition
str(row[38]), # warrenty
str(row[39]), # shipping_cost
category,
]
row_list.append(new_row)
f.close()
with open(csv_path + "/final.csv", 'w') as ff:
writer = csv.writer(ff)
writer.writerows(row_list)
ff.close()
Upvotes: 0
Views: 61
Reputation: 159
Ok so I took a little bit from everyone above to achieve my needs. Thank you all that helped.
csv_row_map = [0, # company
1, # company url
4, # product name
5, # keywords
6, # descripition
7, # sku
8, # manufacturer
13, # saleprice
14, # price
15, # retailprice
17, # buy_link
19, # product_image_url
31, # promotional_text
36, # stock
37, # condition
38, # warrenty
39, # shipping_cost
]
product_to_category_index = {}
for category, products in category_list.items():
product_to_category_index.update(((product.lower(), category) for product in products))
with open(csv_path + '/pre.csv', newline='') as f:
reader = csv.reader(f)
for row in reader:
for k, v in product_to_category_index.items():
if k in row[4].lower():
category = v
break
else:
category = "Uncategorized"
#category = product_to_category_index.get(row[4].lower(), "Uncategorized")
new_row = [row[csv_row_map[i]] for i in range(len(csv_row_map))]
new_row.append(category)
row_list.append(new_row)
with open(csv_path + "/final.csv", 'w') as ff:
writer = csv.writer(ff)
writer.writerows(row_list)
Upvotes: 0
Reputation: 848
Well, this block will only store the result of the last value in val
:
for v in val:
if v.lower() in row[4].lower():
category = k
else:
category = "Uncategorized"
It's only comparing val[-1]
in the end, since you're overwriting category
.
You probably want to break the loop once you find a category, or do something with this value on each iteration?
Upvotes: 3