如何在pyexcel中使用mapdict参数?

时间:2018-03-08 15:04:24

标签: python excel flask-extensions pyexcel

我在pyexcel的save_to_database函数中遇到了mapdict参数的问题。

似乎我仍然需要在我的文件开头有一行列名,否则我会收到错误。 mapdict在将每个列转换为字典后是否指定了用于每个列的名称?

我不确定这个论点实际上是做什么的......

任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:0)

Look, it's simple if you have CSV like this:

brand,sku,description,quantity,price
br,qw3234,s sdf sd ,4,23.5
br,qw3234,s sdf sd ,4,23.5
br,qw3234,s sdf sd ,4,23.5
br,qw3234,s sdf sd ,4,23.5

you don't need mapdict

but if your CSV without first row you need it. For exmple one peace from my flask project:

def article_init_func(row):

    warehouse = Warehouse.query.filter_by(id=id).first()
    a = Article()
    a.pricelist_id = p.id
    a.sku=row['sku']
    a.description=row['description']
    a.brand=row['brand']
    a.quantity=row['quantity']
    a.city=warehouse.city
    a.price=row['price']
    return a

map_row = ['brand', 'sku', 'description', 'quantity', 'price']

request.save_to_database(
    field_name='file', session=db.session,
    initializer = article_init_func,
    table=Article,
    mapdict=map_row)