在两个字符串之间提取一个字符串
extracting a string between 2 strings
我是 python 的新手,我正在尝试使用 python 使用带有 zapier 的代码在 2 个字符串之间提取一个字符串。
示例:dfsgsdfgsdfgsdfgsdfgsdfg 服务:我要提取的内容 客户详细信息:gfdgsdfgsdfgsdfgsdfg
输入字符串称为 'description'
我正在尝试提取字符串 'Service:' 和 'Customer Details:'
之间的内容
我使用了以下代码
import re
match = re.search(r'Service:(.*?)Customer Details:',input_data['description'])
return {'description': match}
测试成功但returns'描述:
空'
我也试过这个代码:
myString=input_data['description']
mySubstring=myString[myString.find("Service:")+8:myString.find("Customer Details:")-17]
return {mySubstring}
我收到错误
'SyntaxError: invalid syntax (usercode.py, line 8)'
如果有人能帮助我,我将不胜感激。
谢谢!
更新 1:
感谢 Abion47 的帮助。我已经输入了以下代码。
import re
input = input_data['description']
match = re.search(r'Service:(.*?)Customer Details:', input).group(1)
print match
我收到以下错误:
Traceback (most recent call last):
File "/tmp/tmpmvAChp/usercode.py", line 10, in the_function match = re.search(r'Service:(.*?)Customer Details:', input).group(1)
AttributeError: 'NoneType' object has no attribute 'group'
更新 2
上面的错误是由于代码没有找到字符串,因此返回了一些空的东西。
这是我的输入文本,它来自 google 日历事件:
Appointment Details
Provider: John Smith
Service: Adult Consultation
Customer Details:
Name: John Doe
Notes: Hi ghdfhdfg, dfghdfgg appointment I had for the 6th of January at 9.30 with this one. Is it possibile?
Status: Confirmed
使用下面的代码我让它工作但我得到的是空值:
import re
name = input_data['description']
print name
try:
try:
name = re.search(r'(?s)(?<=Name:)(.*?)(?=Customer Details:)', input_data['description']).group(1).strip("\n\r ")
except AttributeError:
name = re.search(r'(?s)(?<=Name:)(.*?)(?=Customer Details:)', input_data['description']).group(1)
except AttributeError:
name = re.search(r'(?s)(?<=Name:)(.*?)(?=Customer Details:)', input_data['description'])
return { 'name': name }
但我得到了以下结果,它没有找到我的字符串,即使它在那里!
name: null
runtime_meta
duration_ms: 0
memory_used_mb: 23
logs
1. Appointment Details
2. Provider: John Smith
3. Service: Adult Consultation
4. Customer Details:
5. Name: John Doe
6. Notes: Hi ghdfhdfg, dfghdfgg appointment I had for the 6th of January at 9.30 with this one. Is it possibile?
7. Status: Confirmed
id: vbgOSvUOsBO8tAuLjk4wP0JMsMWsL0WV
如果有人知道代码中有什么问题,将不胜感激!
工作代码
感谢@abion47 的帮助,完整的工作代码是:
import re
name = input_data['description']
print name
myMatch = re.search(r'Service: (.*?)[\r\n]+Customer Details:', name).group(1)
print myMatch
return { 'myMatch': myMatch }
我认为您以错误的方式获取了 String
对象的 find
属性。它 returns 作为输入给出的字符串的第一个字符的索引;通过在字符串对象中找到它。
你的情况;如果你想做到这一点;你可以使用这个:-
>>> myString="dfsgsdfgsdfgsdfgsdfgsdfg Service: what i 'm trying to extract Customer Details: gfdgsdfgsdfgsdfgsdfg"
>>> mySubstring = myString[ myString.find(":")+1 : myString.find("C")-1 ]
>>> mySubstring
" what i 'm trying to extract "
>>>
它所做的只是找到给定字符的索引,然后剥离字符串对象并为您提供所需的结果。
您可以使用 shell 中的以下命令使用 Regex 执行此操作:
input = "dfsgsdfgsdfgsdfgsdfgsdfg Service: what i 'm trying to extract Customer Details: gfdgsdfgsdfgsdfgsdfg"
match = re.search(r'Service:(.*?)Customer Details:', input).group(1)
print match
# Will print " what i 'm trying to extract "
编辑:
这就是为什么第一次在您的问题中 post Minimal, Complete, and Verifiable Example 很重要。如果我们不知道您正在操作的确切数据,那么我们必须做出假设,这很容易出错并导致我们给您无法使用的答案。现在您已经向我们提供了实际的输入数据,我可以立即告诉您为什么您的方法不起作用。
您的子字符串方法(我只能推测,因为您还没有 post 完整的脚本,所以我们无法知道哪个是 "line 8")可能会中断,因为在您之后起始索引加8,结束索引减17,结束索引变得小于起始索引,这是一个错误。
Vicrobot 的子字符串方法是不充分的,因为在您的字符串中有更多的东西可以以 "C" 开头,而不仅仅是 "Customer Details",并且有很多冒号可以与其他冒号匹配尝试(但不在您给我们的示例字符串中)。
你和我的正则表达式方法不起作用,因为你的输入字符串包含需要考虑的换行符,否则正则表达式模式将无法正确匹配。
在这两种情况下您都可以这样处理:
input = '''Appointment Details
Provider: John Smith
Service: Adult Consultation
Customer Details:
Name: John Doe
Notes: Hi ghdfhdfg, dfghdfgg appointment I had for the 6th of January at 9.30 with this one. Is it possibile?
Status: Confirmed'''
# Option 1: Substring
mySubstring = input[ input.find('Service: ')+9 : input.find('\nCustomer Details:') ]
print mySubstring
# Option 2: Regex
import re
myMatch = re.search(r'Service: (.*?)[\r\n]+Customer Details:', input).group(1)
print myMatch
鉴于这两个选项,我会选择 Regex 方法。这是进行文本解析的标准方法,通常不易出错。 (在许多情况下,它也可以比子字符串过滤器运行得更快,我怀疑这是其中之一。)
我是 python 的新手,我正在尝试使用 python 使用带有 zapier 的代码在 2 个字符串之间提取一个字符串。 示例:dfsgsdfgsdfgsdfgsdfgsdfg 服务:我要提取的内容 客户详细信息:gfdgsdfgsdfgsdfgsdfg 输入字符串称为 'description' 我正在尝试提取字符串 'Service:' 和 'Customer Details:'
之间的内容我使用了以下代码
import re
match = re.search(r'Service:(.*?)Customer Details:',input_data['description'])
return {'description': match}
测试成功但returns'描述: 空'
我也试过这个代码:
myString=input_data['description']
mySubstring=myString[myString.find("Service:")+8:myString.find("Customer Details:")-17]
return {mySubstring}
我收到错误 'SyntaxError: invalid syntax (usercode.py, line 8)'
如果有人能帮助我,我将不胜感激。 谢谢!
更新 1: 感谢 Abion47 的帮助。我已经输入了以下代码。
import re
input = input_data['description']
match = re.search(r'Service:(.*?)Customer Details:', input).group(1)
print match
我收到以下错误:
Traceback (most recent call last):
File "/tmp/tmpmvAChp/usercode.py", line 10, in the_function match = re.search(r'Service:(.*?)Customer Details:', input).group(1)
AttributeError: 'NoneType' object has no attribute 'group'
更新 2 上面的错误是由于代码没有找到字符串,因此返回了一些空的东西。
这是我的输入文本,它来自 google 日历事件:
Appointment Details
Provider: John Smith
Service: Adult Consultation
Customer Details:
Name: John Doe
Notes: Hi ghdfhdfg, dfghdfgg appointment I had for the 6th of January at 9.30 with this one. Is it possibile?
Status: Confirmed
使用下面的代码我让它工作但我得到的是空值:
import re
name = input_data['description']
print name
try:
try:
name = re.search(r'(?s)(?<=Name:)(.*?)(?=Customer Details:)', input_data['description']).group(1).strip("\n\r ")
except AttributeError:
name = re.search(r'(?s)(?<=Name:)(.*?)(?=Customer Details:)', input_data['description']).group(1)
except AttributeError:
name = re.search(r'(?s)(?<=Name:)(.*?)(?=Customer Details:)', input_data['description'])
return { 'name': name }
但我得到了以下结果,它没有找到我的字符串,即使它在那里!
name: null
runtime_meta
duration_ms: 0
memory_used_mb: 23
logs
1. Appointment Details
2. Provider: John Smith
3. Service: Adult Consultation
4. Customer Details:
5. Name: John Doe
6. Notes: Hi ghdfhdfg, dfghdfgg appointment I had for the 6th of January at 9.30 with this one. Is it possibile?
7. Status: Confirmed
id: vbgOSvUOsBO8tAuLjk4wP0JMsMWsL0WV
如果有人知道代码中有什么问题,将不胜感激!
工作代码
感谢@abion47 的帮助,完整的工作代码是:
import re
name = input_data['description']
print name
myMatch = re.search(r'Service: (.*?)[\r\n]+Customer Details:', name).group(1)
print myMatch
return { 'myMatch': myMatch }
我认为您以错误的方式获取了 String
对象的 find
属性。它 returns 作为输入给出的字符串的第一个字符的索引;通过在字符串对象中找到它。
你的情况;如果你想做到这一点;你可以使用这个:-
>>> myString="dfsgsdfgsdfgsdfgsdfgsdfg Service: what i 'm trying to extract Customer Details: gfdgsdfgsdfgsdfgsdfg"
>>> mySubstring = myString[ myString.find(":")+1 : myString.find("C")-1 ]
>>> mySubstring
" what i 'm trying to extract "
>>>
它所做的只是找到给定字符的索引,然后剥离字符串对象并为您提供所需的结果。
您可以使用 shell 中的以下命令使用 Regex 执行此操作:
input = "dfsgsdfgsdfgsdfgsdfgsdfg Service: what i 'm trying to extract Customer Details: gfdgsdfgsdfgsdfgsdfg"
match = re.search(r'Service:(.*?)Customer Details:', input).group(1)
print match
# Will print " what i 'm trying to extract "
编辑:
这就是为什么第一次在您的问题中 post Minimal, Complete, and Verifiable Example 很重要。如果我们不知道您正在操作的确切数据,那么我们必须做出假设,这很容易出错并导致我们给您无法使用的答案。现在您已经向我们提供了实际的输入数据,我可以立即告诉您为什么您的方法不起作用。
您的子字符串方法(我只能推测,因为您还没有 post 完整的脚本,所以我们无法知道哪个是 "line 8")可能会中断,因为在您之后起始索引加8,结束索引减17,结束索引变得小于起始索引,这是一个错误。
Vicrobot 的子字符串方法是不充分的,因为在您的字符串中有更多的东西可以以 "C" 开头,而不仅仅是 "Customer Details",并且有很多冒号可以与其他冒号匹配尝试(但不在您给我们的示例字符串中)。
你和我的正则表达式方法不起作用,因为你的输入字符串包含需要考虑的换行符,否则正则表达式模式将无法正确匹配。
在这两种情况下您都可以这样处理:
input = '''Appointment Details
Provider: John Smith
Service: Adult Consultation
Customer Details:
Name: John Doe
Notes: Hi ghdfhdfg, dfghdfgg appointment I had for the 6th of January at 9.30 with this one. Is it possibile?
Status: Confirmed'''
# Option 1: Substring
mySubstring = input[ input.find('Service: ')+9 : input.find('\nCustomer Details:') ]
print mySubstring
# Option 2: Regex
import re
myMatch = re.search(r'Service: (.*?)[\r\n]+Customer Details:', input).group(1)
print myMatch
鉴于这两个选项,我会选择 Regex 方法。这是进行文本解析的标准方法,通常不易出错。 (在许多情况下,它也可以比子字符串过滤器运行得更快,我怀疑这是其中之一。)