0

I'm not an expert in python and since it's work related i may have to change some variables and info.

So i have a long string from an API request in which I'm looking for the information of users, and to make sure i only have the users info , I use their ID with .find(ID) which show me the index where the ID is in the string ( middle of the user info). After that what i want is to go up the

string and search for the tag that starts the information 
`**<resource>** and down to the tag that end the information ****</resource>**.` 

**id_position = string.find(id)**
**upper = string[:id_position].rfind("<resource>")** to go in reverse order up the string and find the first match , and works fine

but when i use **lower = string[id_position].find("</resource>")**

, instead of going normally from the id position,down till it finds the first match , it actually searches in the entire original string, as in searching from the first position string[0] onwards.

when i print

**string[:id_position] + string[id_position:] == string** ,

it shows it's true, so i'm guessing the find function doesn't help me like i think it should.

So my question is, how can i search for my specific substring after the index of the id ?

I know it's hard to understand but i hope some of you may know what i mean

for reference,the data looks like this

.<resource>
   name=
   ip=
.</resource>

.<resource>
   name2=
   ip2=
</resource>

.<resource>
   name=
   ip2=
.</resource>
  • Does this answer your question? [How do I get a substring of a string in Python?](https://stackoverflow.com/questions/663171/how-do-i-get-a-substring-of-a-string-in-python) – zerocukor287 Jan 20 '22 at 14:56
  • 2
    Have you tried using the [`start`](https://docs.python.org/3/library/stdtypes.html#str.find) parameter of `str.find`? – 0x5453 Jan 20 '22 at 14:56
  • Please add a more complete example of your string so that it is possible to test code with? Furthermore, when you say "ID", do you mean "name" or "ip"? And is there a difference between "ip"/"name" and "ip2"/"name2"? – eandklahn Jan 20 '22 at 15:43

1 Answers1

0

Have you tried using find()s optional parameters start and end?

id_position = string.find(id)
upper = string.find("</resource>", id_position) + len("</resource>")
lower = string.rfind("<resource>", 0, id_position)

print(repr(string[lower:upper]))
> '<resource>\n   name2=\n   ip2=\n</resource>'
  • When i use them i get "name 'start' / 'end 'is not defined" – Valentin Florea Jan 20 '22 at 15:12
  • I would use the comment section to ask clarifying questions such as this. Once you have all the information you could create a great answer explaining why the proposed solution works or solves this particular problem. This would help others to understand the why and learn from it. Hope this feedback helps :) – MrBlaise Jan 20 '22 at 15:55
  • @MrBlaise I don't have enough reputation to comment yet except on my own answers ^^. – felix.goroncy Jan 20 '22 at 15:58
  • @ValentinFlorea `start` and `end` are not defined because they are declared as positional parameters only. so `string.find(id, start=id_position)` will fail while `string.find(id, id_position)` should work. – felix.goroncy Jan 20 '22 at 16:30
  • Thank you for the explanation @felix.goroncy. worked fine like you explained it , also i found a more barebones example: https://www.programiz.com/python-programming/methods/string/find for people that are as unexperienced as me in Python. – Valentin Florea Jan 21 '22 at 09:24