【问题标题】:Zapier gives error: `'unicode' object has no attribute 'copy'` for Python scriptZapier 给出错误:Python 脚本的“unicode”对象没有属性“copy”
【发布时间】:2019-06-14 02:31:04
【问题描述】:

脚本很简单:

import datetime
import json

today = datetime.date.today()
next_thursday = today + datetime.timedelta(((3 - today.weekday()) % 7))
while True:
    if 15 <= next_thursday.day <= 21:
        next_third_thursday = next_thursday
        break
    else:
        next_date = next_thursday + datetime.timedelta(days=1)
        next_thursday = next_date + datetime.timedelta(((3 - next_date.weekday()) % 7))

return json.dumps({'date': str(next_third_thursday)})

如何让这段代码运行?这里有什么问题?

【问题讨论】:

    标签: python json python-2.7 datetime zapier


    【解决方案1】:

    Zapier 期望脚本的输出是 JSON 可序列化对象(来自 cmets 部分的 Michael Case)。

    此外,脚本没有正确缩进。 Python 是一种对缩进敏感的语言,即缩进很重要。

    试试这样的:

    import datetime
    
    today = datetime.date.today()
    next_thursday = today + datetime.timedelta(((3 - today.weekday()) % 7))
    while True:
        if 15 <= next_thursday.day <= 21:
            next_third_thursday = next_thursday
            break
        else:
            next_date = next_thursday + datetime.timedelta(days=1)
            next_thursday = next_date + datetime.timedelta(((3 - next_date.weekday()) % 7))
    
    return {'date': str(next_third_thursday)}
    

    【讨论】:

    • 我已经更新了答案。我认为您可能还需要正确缩进代码。
    • 愚蠢的我。我确实缩进了代码,但错误地粘贴了没有缩进的代码。不幸的是......您更新的答案仍然返回相同的错误。 :( 啊。真令人沮丧。
    • 哦,错误返回只是显示代码未缩进,但运行时正确缩进。
    • 米兰的答案很接近。这里的问题是 json.dumps 序列化您的对象,将其转换为字符串。因此,在此示例中,您仍然返回一个字符串,这就是您收到相同错误的原因。使用 Zapier 的代码模块,您需要返回一个字典对象,Zapier 会自动将其映射为 JSON 格式以供后续步骤使用。在这种情况下,删除 json.dumps 行并简单地返回字典对象。
    • 我已经更新了米兰的答案,以便为将来可能遇到它的任何人产生期望的结果。
    猜你喜欢
    • 2023-03-16
    • 2013-01-18
    • 2013-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-12
    • 2014-05-14
    • 1970-01-01
    相关资源
    最近更新 更多