【问题标题】:How to correctly return a list of dictionaries in Zapier Code (Python)?如何正确返回 Zapier Code (Python) 中的字典列表?
【发布时间】:2018-05-02 01:37:57
【问题描述】:

Zapier 代码文档说代码 zap 的输出可以是字典或字典列表(请参阅“数据变量”部分:https://zapier.com/help/code-python/)。

这样做时,

output = [{'Booking':'Shirt'},{'Booking':'Jeans'}]

代码的输出只返回第一个字典,但是:

runtime_meta__duration_ms:  2
runtime_meta__memory_used_mb:   22
id: [redacted]
Booking:    Shirt
Fields with no value:
runtime_meta__logs

我在这里做错了什么?非常感谢!

【问题讨论】:

    标签: zapier


    【解决方案1】:

    来自 Zapier 平台团队的 David 在这里。返回数组的代码步骤大部分是未记录的(因为没有 UI 支持,而且正如您所知,它令人困惑)功能。

    在测试时,它只会显示数组中的第一项。当它真正运行时,代码步骤之后的所有步骤都将为数组中的每个项目运行。任务历史会反映这一点

    所以设置 zap 并打开它,它会像你期望的那样工作。

    抱歉,如果您有任何其他问题,请告诉我!

    【讨论】:

    • 这仍然让我感到困惑,我真的需要帮助来理解。在撰写本文时,我对文档失去了信心。我们是否需要编写一个返回输出变量的返回语句?我们是否设置了变量输出并且不必担心制作return语句?是否需要设置名为 output 的变量,或者这无关紧要?我们可以返回任何变量作为输出还是不是这样工作的?文档以编程和上下文的方式使用返回和输出这两个词。这是我困惑的根源。
    • 很抱歉让您感到困惑。答案是两种方法都有效 - Zapier 在您的代码运行后运行return output(如果该函数尚未返回)。所以有两个选择:1.return {'my': 'data'}; 2.output = {'my': 'data'}。每个都是等价的。
    • 非常感谢您多年后的回复。所以,赞!这对我有帮助!
    【解决方案2】:

    对于仍在寻找此问题答案的任何人,以下是在 Zapier 中查找返回列表的内容。

    # first import and convert your input value to an array.
    # special note any line items imported into a python variable are converted to list format.
    my_items = input_data['my_CSV_string']
    my_list_of_items = my_items.split(",")
    
    # Create a new list array 
    my_new_list = []
    
    length = len(my_list_of_items)
    #Do all your computations    
    for i in range(length): 
        my_new_list.append(float(my_list_of_items[i])*1.5)
    
    # After completing any tasks you can return the list as follows, 
    # If you are using line items keep the list in its original format
    return {
        'my_processed_values': my_new_list,
        'original_values': my_list_of_items
    }
    
    # If you want to return it as a CSV "basically making the array flat"
    my_old_CSV_list= ','.join(map(str, my_list_of_items))
    my_new_CSV_list= ','.join(map(str, my_new_list))
    return {
        'my_processed_cvs_values': my_new_CSV_list,
        'original_values': my_list_of_items
    }
    

    希望这会有所帮助。我不是 Python 专家,但理论上使用的列表越多,zap 处理的时间就越长。尽量将你的 python 处理时间保持在最低水平。

    最好的,

    【讨论】:

      猜你喜欢
      • 2018-11-18
      • 2022-01-15
      • 1970-01-01
      • 2012-12-04
      • 2013-05-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多