Reputation: 259
Not seeing my answer so I'm going to explain what I'm looking to do. I need to break some data out of a url, some of the urls have the word limit in the followed by a guid. What I want to do is see if the word is in any part of the item in the list.
I want to iterate through the list items and if the item contains the word limit I want to get the next 33 characters
Here is an example of the list:
limits = ['limit=5e8f0dc0-4074-481d-a12c-5d3a70406ad7&location=d877f1f9-fae3-4f39-82ca-499ec8a7a0d2', 'location=d877f1f9-fae3-4f39-82ca-499ec8a7a0d2', 'sourceLimitAllocationOid=212a7e22-aafd-40e3-9684-621dae6708af&sourceUnits=5000&sourceUnitOfMeasureCode=TON&tragetUnitOfMeasureOid=1bec1009-90d3-de11-9251-001d092afdd0&targetLimitCommodityOid=7e7a1112-b7c7-4563-9bd4-702e14b139f4', '', 'limit=5e8f0dc0-4074-481d-a12c-5d3a70406ad7&location=2e12899b-e84d-4b1a-8b89-faced0771efc', '', 'location=2e12899b-e84d-4b1a-8b89-faced0771efc', 'sourceLimitAllocationOid=bfa24508-a4e3-4138-8d29-7e9b7c1ea559&sourceUnits=3000000&sourceUnitOfMeasureCode=BU&tragetUnitOfMeasureOid=1bec1009-90d3-de11-9251-001d092afdd0&targetLimitCommodityOid=7e7a1112-b7c7-4563-9bd4-702e14b139f4', '', '', 'limit=c0ba884c-800b-4174-8374-0bc5515f8282&location=b3d785e6-4d0c-460b-8e2c-1f32732a1a20', 'location=b3d785e6-4d0c-460b-8e2c-1f32732a1a20']
Upvotes: 1
Views: 47
Reputation: 7980
According to this tutorial, you can do this:
offset = len("limit")
limit = ""
for item in limits:
index = item.find("limit")
if (index != -1):
limit = item[ (limit + offset + 1): (limit + offset + 34) ]
break
There's a couple of things going on here on top of what you asked for. First, I assumed that there's only one string with limit
in it. If not, remove the break
statement and make limit
be a list with the assignment changed to an append
. Second, I assumed you didn't want to take the =
from the string so I'm actually adding one to the start and end to do that. Alternatively, you could just remove the first character later if that's what you want to do.
I hope this helps.
Upvotes: 1
Reputation: 527
Try this:
import re
limits = ['limit=5e8f0dc0-4074-481d-a12c-5d3a70406ad7&location=d877f1f9-fae3-4f39-82ca-499ec8a7a0d2', 'location=d877f1f9-fae3-4f39-82ca-499ec8a7a0d2', 'sourceLimitAllocationOid=212a7e22-aafd-40e3-9684-621dae6708af&sourceUnits=5000&sourceUnitOfMeasureCode=TON&tragetUnitOfMeasureOid=1bec1009-90d3-de11-9251-001d092afdd0&targetLimitCommodityOid=7e7a1112-b7c7-4563-9bd4-702e14b139f4', '', 'limit=5e8f0dc0-4074-481d-a12c-5d3a70406ad7&location=2e12899b-e84d-4b1a-8b89-faced0771efc', '', 'location=2e12899b-e84d-4b1a-8b89-faced0771efc', 'sourceLimitAllocationOid=bfa24508-a4e3-4138-8d29-7e9b7c1ea559&sourceUnits=3000000&sourceUnitOfMeasureCode=BU&tragetUnitOfMeasureOid=1bec1009-90d3-de11-9251-001d092afdd0&targetLimitCommodityOid=7e7a1112-b7c7-4563-9bd4-702e14b139f4', '', '', 'limit=c0ba884c-800b-4174-8374-0bc5515f8282&location=b3d785e6-4d0c-460b-8e2c-1f32732a1a20', 'location=b3d785e6-4d0c-460b-8e2c-1f32732a1a20']
for el in limits:
a = re.search(r'\b(limit)\b', el)
if a is not None:
ind = a.start()
print(el[ind+5:ind+38])
Upvotes: 2