Zapier:代码没有返回预期的所有值

时间:2022-10-09 20:27:04

I am working with Code by Zapier and having trouble telling if my regex is wrong or some other part is wrong ( I think the latter)

我正在使用Zapier的Code并且无法判断我的正则表达式是错还是其他部分错了(我认为后者)

I am pulling a URL, this URL has several 9 digit IDs that get appended to the end of the URL. I was told to try and extract these IDs and rebuild the URLs so we can post API calls for each of them.

我正在提取一个URL,这个URL有几个9位ID,这些ID会附加到URL的末尾。我被告知尝试提取这些ID并重建URL,以便我们可以为每个ID发布API调用。

I am a Python newb but so far I have this but it only returns the first 9 digit ID, I am hoping for an array so I can rebuild the URLS with each specific ID. This is my code so far:

我是一个Python newb,但到目前为止,我有这个,但它只返回前9位ID,我希望有一个数组,所以我可以重建每个特定ID的URLS。到目前为止这是我的代码:

import re
    urls = re.findall(r'\b\d+\b', input_data['input'])
         return [
             {'url': url} for url in urls
]

The input _data would be "https://api.helpscout.net/v1/conversations/123456789,098765432.json"

输入_data将是“https://api.helpscout.net/v1/conversations/123456789,098765432.json”

As I said, it just returns the first ID. I know I don't have the code to rebuild the URLs or anything just trying to take it one step at a time!

正如我所说,它只返回第一个ID。我知道我没有重建URL的代码或任何只是尝试一步一步的代码!

IS my regex incorrect or the way I am returning them? Thanks!

我的正则表达式是不正确的还是我回复它们的方式?谢谢!

3 个解决方案

#1


0  

The code works correct on my machine:

代码在我的机器上正常工作:

import re

def get_digits(s):
    return [{'url':url} for url in re.findall(r'\b\d+\b',s)]

If I then call this with the sample input, I get:

如果我然后用样本输入调用它,我得到:

>>> get_digits("https://api.helpscout.net/v1/conversations/123456789,098765432.json")
[{'url': '123456789'}, {'url': '098765432'}]

So a list with two dictionaries. Each dictionary contains one key, a 'url' that is associated with the string that contains one or more digits.

这是一个包含两个词典的列表。每个字典包含一个键,一个与包含一个或多个数字的字符串相关联的“url”。

In case you want to match only nine-digit sequences, you can make the regex more restrictive (but this can only decrease the number of matches):

如果您只想匹配九位数序列,可以使正则表达式更具限制性(但这只会减少匹配数):

import re

def get_digits(s):
    return [{'url':url} for url in re.findall(r'\b\d{9}\b',s)]

#2


0  

Argh so frustrating, I decided to try JavaScript and multiple methods don't output anything

唉这么沮丧,我决定尝试JavaScript,多种方法都不输出任何东西

<script>
var str = "https://api.helpscout.net/v1/conversations/382411278,374879346,374879343.json";
var tickets = str.match(/\d{9}/g);
for(var i = 0; i<tickets.length; i++)
    {
document.write("https://api.helpscout.net/v1/conversations/"+tickets[i]+".json</br>")
}

or

<p id="demo"></p>
<script>
function myFunction() {
    var str = "https://api.helpscout.net/v1/conversations/382411278,374879346,374879343.json";
    var tickets = str.match(/\d{9}/g);

    for(var i = 0; i<tickets.length; i++)
    {
      document.getElementById("demo").innerHTML +="https://api.helpscout.net/v1/conversations/" +tickets[i] + "<br />"
    }

}
</script>

#3


0  

David here, from the Zapier Platform team. I've got good news and bad news.

大卫来自Zapier平台团队。我有好消息和坏消息。

Good news is, your regex works! So no sweat there. Downside, you're running into a weird corner case with code steps. It's not documented because we don't encourage its use (it's confusing, as you can tell). When you return an array from a code step, it functions like a trigger. That is, subsequent steps run for each item in the array (but the UI only shows the first).

好消息是,你的正则表达式有效!所以没有汗水。在下面,你遇到了一个带有代码步骤的怪异角落。它没有记录,因为我们不鼓励它的使用(这是令人困惑的,你可以告诉)。从代码步骤返回数组时,它的作用类似于触发器。也就是说,后续步骤针对阵列中的每个项目运行(但UI仅显示第一个)。

If that's the desired behavior, you can safely ignore the weird test and complete the zap. If you don't want to fan out, you should instead parse out the comma separated string and act on it later.

如果这是所需的行为,您可以放心地忽略奇怪的测试并完成zap。如果您不想散开,则应该解析逗号分隔的字符串并稍后对其进行操作。

If you need more direction, let me know a bit about what your other actions are and I can advise from there.

如果您需要更多指导,请让我知道您的其他行为是什么,我可以从那里提出建议。

Side note, the reason you're seeing the error message with Willem's function above is that your python code must either set the output variable to a value or return an object. Either of return get_digits(input_data['input']) or output = get_digits(input_data['input']) should work.

旁注,你看到上面Willem函数的错误消息的原因是你的python代码必须将输出变量设置为一个值或返回一个对象。返回get_digits(input_data ['input'])或output = get_digits(input_data ['input'])应该有效。

#1


0  

The code works correct on my machine:

代码在我的机器上正常工作:

import re

def get_digits(s):
    return [{'url':url} for url in re.findall(r'\b\d+\b',s)]

If I then call this with the sample input, I get:

如果我然后用样本输入调用它,我得到:

>>> get_digits("https://api.helpscout.net/v1/conversations/123456789,098765432.json")
[{'url': '123456789'}, {'url': '098765432'}]

So a list with two dictionaries. Each dictionary contains one key, a 'url' that is associated with the string that contains one or more digits.

这是一个包含两个词典的列表。每个字典包含一个键,一个与包含一个或多个数字的字符串相关联的“url”。

In case you want to match only nine-digit sequences, you can make the regex more restrictive (but this can only decrease the number of matches):

如果您只想匹配九位数序列,可以使正则表达式更具限制性(但这只会减少匹配数):

import re

def get_digits(s):
    return [{'url':url} for url in re.findall(r'\b\d{9}\b',s)]

#2


0  

Argh so frustrating, I decided to try JavaScript and multiple methods don't output anything

唉这么沮丧,我决定尝试JavaScript,多种方法都不输出任何东西

<script>
var str = "https://api.helpscout.net/v1/conversations/382411278,374879346,374879343.json";
var tickets = str.match(/\d{9}/g);
for(var i = 0; i<tickets.length; i++)
    {
document.write("https://api.helpscout.net/v1/conversations/"+tickets[i]+".json</br>")
}

or

<p id="demo"></p>
<script>
function myFunction() {
    var str = "https://api.helpscout.net/v1/conversations/382411278,374879346,374879343.json";
    var tickets = str.match(/\d{9}/g);

    for(var i = 0; i<tickets.length; i++)
    {
      document.getElementById("demo").innerHTML +="https://api.helpscout.net/v1/conversations/" +tickets[i] + "<br />"
    }

}
</script>

#3


0  

David here, from the Zapier Platform team. I've got good news and bad news.

大卫来自Zapier平台团队。我有好消息和坏消息。

Good news is, your regex works! So no sweat there. Downside, you're running into a weird corner case with code steps. It's not documented because we don't encourage its use (it's confusing, as you can tell). When you return an array from a code step, it functions like a trigger. That is, subsequent steps run for each item in the array (but the UI only shows the first).

好消息是,你的正则表达式有效!所以没有汗水。在下面,你遇到了一个带有代码步骤的怪异角落。它没有记录,因为我们不鼓励它的使用(这是令人困惑的,你可以告诉)。从代码步骤返回数组时,它的作用类似于触发器。也就是说,后续步骤针对阵列中的每个项目运行(但UI仅显示第一个)。

If that's the desired behavior, you can safely ignore the weird test and complete the zap. If you don't want to fan out, you should instead parse out the comma separated string and act on it later.

如果这是所需的行为,您可以放心地忽略奇怪的测试并完成zap。如果您不想散开,则应该解析逗号分隔的字符串并稍后对其进行操作。

If you need more direction, let me know a bit about what your other actions are and I can advise from there.

如果您需要更多指导,请让我知道您的其他行为是什么,我可以从那里提出建议。

Side note, the reason you're seeing the error message with Willem's function above is that your python code must either set the output variable to a value or return an object. Either of return get_digits(input_data['input']) or output = get_digits(input_data['input']) should work.

旁注,你看到上面Willem函数的错误消息的原因是你的python代码必须将输出变量设置为一个值或返回一个对象。返回get_digits(input_data ['input'])或output = get_digits(input_data ['input'])应该有效。