【问题标题】:How to convert values in table using petl如何使用petl转换表中的值
【发布时间】:2018-04-15 11:10:40
【问题描述】:

我在转换表中的多个值时遇到了一个奇怪的问题。

我有一个table 的数据,如下所示:

+-------------+--------------+-------------+
| id          | name         | category_id |
+=============+==============+=============+
|         1   |       Horse  | 5           |
+-------------+--------------+-------------+
|         2   |        Cow   | 5           |
+-------------+--------------+-------------+
|         3   |        Pig   | 2           |
+-------------+--------------+-------------+
|         4   |     Chicken  | 3           |
+-------------+--------------+-------------+

然后我通过category_id 查找如下:

# find the item category id
items_cat_id = etl.values(table, 'category_id')

然后我像这样循环数据,这样我就可以将上面的 category_id 转换为目标类别 id:

    for item in items_cat_id:
        """ 
        fetch target mongo collection. 
        source_name is the category name to look up in the target, 
        if we get a match convert the table category_id value to 
        the mongo id.
        """
        target_category_id = target_db.collection.find_one({ 'name': source_name })

        converted_table = etl.convert(table, 'category_id', 
            lambda _: target_category_id.get('_id'), 
            where=lambda x: x.category_id == item)

我似乎只得到这个:

+-------------+--------------+-----------------------------+
| id          | name         | category_id                 |
+=============+==============+=============================+  
|         1   |       Horse  | 5                           |
+-------------+--------------+-----------------------------+
|         2   |        Cow   | 5                           |
+-------------+--------------+-----------------------------+
|         3   |        Pig   | 2                           |
+-------------+--------------+-----------------------------+
|         4   |     Chicken  | QnicP3f4njL54HRqu           |
+-------------+--------------+-----------------------------+

应该是什么时候

+-------------+--------------+-----------------------------+
| id          | name         | category_id                 |
+=============+==============+=============================+
|         1   |       Horse  | 5                           |
+-------------+--------------+-----------------------------+
|         2   |        Cow   | 5                           |
+-------------+--------------+-----------------------------+
|         3   |        Pig   | yrDku5Yqkc2MKZZkD           |
+-------------+--------------+-----------------------------+
|         4   |     Chicken  | QnicP3f4njL54HRqu           |
+-------------+--------------+-----------------------------+

有什么建议吗?

【问题讨论】:

    标签: python etl petl


    【解决方案1】:

    etl.convert() 使用相同的未更改源 tableitems_cat_id 的每次迭代创建一个新表。因此,每个结果中仅更改一行。像这样更改您的代码:

    for x in y:
       table = etl.convert(table, ...)
    

    现在你总是在处理最后一个结果。

    【讨论】:

    • 是的,前段时间有人问过这个问题,但我发现 convert 每次都会创建一个新表 :)
    猜你喜欢
    • 2018-11-17
    • 1970-01-01
    • 1970-01-01
    • 2012-12-23
    • 2018-09-18
    • 1970-01-01
    • 2019-11-05
    • 1970-01-01
    • 2018-11-16
    相关资源
    最近更新 更多