首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将python追加到列表中

将python追加到列表中
EN

Stack Overflow用户
提问于 2012-11-13 17:37:02
回答 1查看 376关注 0票数 0

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

代码语言:javascript
复制
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"]

举个例子:

代码语言:javascript
复制
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

什么是将product_ref映射到import_fields列表中的引用的正确方法,而所有缺少的值都留空,或者创建一个csv,以便当我追加一个值时,它被添加到正确的列中?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-11-13 17:39:38

使用csv DictWriterDictReader

http://docs.python.org/2/library/csv.html#csv.DictWriter

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13358368

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档