Python 意外返回的循环字典
这是一个漫长的夜晚和白天。我需要一些帮助,为什么这不起作用,我不能直接思考。此脚本打开一个.csv文件,检查某个单词是否包含在第[4]行标题中,然后它将在包含列表的字典中循环,如果该单词位于标题中,则它将返回要写入新csv文件的字典键 我得到的每一行都没有分类,这不应该是正确的,因为标题确实包含列表中的单词 提前谢谢你的帮助 BrandsMart USA中的CSV示例,产品目录,01:30.8,Mini Bass King Jr蓝牙便携式户外扬声器,2Boom BX320K,2Boom BX320K Mini Bass King Jr蓝牙便携式户外扬声器,2boomX320K,2Boom,BX320K,7.08192E+11,,美元,19.88,19.88,,, BrandsMart USA,产品目录,01:30.8,Mini Bass King Jr蓝牙便携式室外扬声器,2Boom BX320R,2Boom BX320R Mini Bass King Jr蓝牙便携式室外扬声器,2boomX320R,2Boom,BX320R,7.08192E+11,,美元,19.88,19.88,,, BrandsMart USA,产品目录,01:30.8,蓝牙降噪耳塞耳机,2Boom EPBT690B,2Boom EPBT690B蓝牙降噪耳塞耳机,2Boom EPBT690B,2Boom,EPBT690B,7.44751E+11,,美元,9.88,9.88 使用Python3Python 意外返回的循环字典,python,python-3.x,Python,Python 3.x,这是一个漫长的夜晚和白天。我需要一些帮助,为什么这不起作用,我不能直接思考。此脚本打开一个.csv文件,检查某个单词是否包含在第[4]行标题中,然后它将在包含列表的字典中循环,如果该单词位于标题中,则它将返回要写入新csv文件的字典键 我得到的每一行都没有分类,这不应该是正确的,因为标题确实包含列表中的单词 提前谢谢你的帮助 BrandsMart USA中的CSV示例,产品目录,01:30.8,Mini Bass King Jr蓝牙便携式户外扬声器,2Boom BX320K,2Boom BX32
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()
那么,此块将只存储
val
中最后一个值的结果:
for v in val:
if v.lower() in row[4].lower():
category = k
else:
category = "Uncategorized"
它最后只是比较val[-1]
,因为您正在覆盖类别
您可能希望在找到类别后中断循环,或者在每次迭代中使用此值执行某些操作?好的,此块将只在val
中存储最后一个值的结果:
for v in val:
if v.lower() in row[4].lower():
category = k
else:
category = "Uncategorized"
它最后只是比较val[-1]
,因为您正在覆盖类别
一旦你找到一个类别,你可能想打破循环,或者在每次迭代中用这个值做一些事情?好吧,所以我从上面的每个人身上取了一点来满足我的需要。谢谢你的帮助
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)
好的,所以我从上面的每个人身上取了一点来满足我的需求。谢谢你的帮助
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)
您能给出一个csv中您希望分类的示例行吗?除了添加示例行之外,不需要str(行[0])
,它已经是一个字符串了。或者f.close()
,with
子句会自动执行此操作。我必须编写str(第[0]行),因为我在列上遇到了一个错误,列上写着“int base 10”,这是我运行它的唯一方法。您可以从csv中给出一个您希望分类的示例行吗?除了添加示例之外,不需要str(第[0]行)
,它已经是一个字符串了。或者f.close()
,with
子句会自动执行此操作。我必须编写str(行[0]),因为我在列上遇到一个错误,该列指出“int base 10”,这是我运行它的唯一方法