简体   繁体   English

使用 python 在 Zapier 中的字符串中的字符之间提取数据

[英]Extract data in between characters within string in Zapier using python

I am working to extract the twitter handle of a user from a string of text.我正在从一串文本中提取用户的 twitter 句柄。

The text is always in this format:文本始终采用以下格式:

SomeText (@handle) SomeText (@handle)

BTW, I am running this as a step in a multi-step zap.顺便说一句,我将其作为多步骤 zap 中的一个步骤运行。 My idea was to use python to extract everything in between the parenthesis using this code:我的想法是使用 python 使用以下代码提取括号之间的所有内容:

s = input_data['s']
return s[s.find("(")+1:s.find(")")]

I defined s in my zap per the documentation, see screen shot:我根据文档在我的 zap 中定义了 s,请参见屏幕截图:

在此处输入图像描述

I am getting the following error:我收到以下错误:

'str' object has no attribute 'copy' 'str' object 没有属性 'copy'

Use a regular expression, as in:使用正则表达式,如:

import re

text1 = "SomeText (@handle)"
text2 = "1 (bla bla) article #author: #something (@handle)"

res = re.search(r"\(([^)]*)\)$", text2)
res.groups()[0]

The result is '@handle' for both text1 and text2.对于 text1 和 text2,结果都是“@handle”。

Your issue is that data returned from a Python Code step must be JSON serializable.您的问题是从 Python 代码步骤返回的数据必须是 JSON 可序列化的。

try this:尝试这个:

s = input_data['s']
return {'handle': s[s.find("(")+1:s.find(")")]}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM