map python追加到列表中

时间:2012-11-13 09:37:02

标签: python beautifulsoup

我正在尝试在python中创建一个csv文件并将字段映射到一个预先存在的字段,这里是我的csv的标题文件

import_fields = ["ID", "Active (0/1)", "Name *", "Categories (x,y,z...)", 
                "Price tax excl. or Price tax incl.", "Tax rules ID", "Wholesale price", "On sale (0/1)", 
                "Discount amount", "Discount percent", "Discount from (yyyy-mm-dd)", "Discount to (yyyy-mm-dd)", 
                "Reference #", "Supplier reference #", "Supplier", "Manufacturer", "EAN13", "UPC", "Ecotax", "Weight", 
                "Quantity", "Short description", "Description", "Tags (x,y,z...)", "Meta-title", "Meta-keywords", 
                "Meta-description", "URL rewritten", "Text when in stock", "Text when backorder allowed", 
                "Available for order (0 = No, 1 = Yes)", "Product creation date", "Show price (0 = No, 1 = Yes)", 
                "Image URLs (x,y,z...)", "Delete existing images (0 = No, 1 = Yes)", "Feature(Name:Value:Position)", 
                "Available online only (0 = No, 1 = Yes)", "Condition", "ID / Name of shop"]

所以例如:

adm_product = []
for category in breadcrumbs.findAll('li', { "class" : re.compile(r'\bcategory\d')}):
    adm_product.append(category.find('a').renderContents())

product_shop = soup.find('div', attrs={"class": "product-shop"})
product_sku = soup.find('p', attrs={"class": "product-sku"})
if product_sku:
    sku = product_sku.renderContents()
    product_ref = ref(sku)[0]
    adm_product.append(product_ref) # MAP TO REFERENCE

short_description = soup.find('div', attrs={"class": "short-description"})
if short_description:
    short_desc = short_description.find('div', attrs={"class": "std"})
    if short_desc:
        adm_product.append(short_desc.renderContents()) # MAP TO SHORT DESCRIPTION

在import_fields列表中将product_ref映射到Reference的正确方法是什么,将任何缺失值留空或创建csv,以便在我追加值时将其添加到正确的列中?

1 个答案:

答案 0 :(得分:3)