Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Python 

Using Strip in Python

Is there anyway using either the strip or split function to remove everything up to the ">" ?

for example

<city>Ohama</city>

s.strip("< ").strip(" > \n").strip("/")

'city>Ohama</city'

Gets me that, I can remove the first < and last > but is there a way to take away the "city> and </city?
So you just want the city name without the tags?

PHP Code

<?php 
city 
= "<city>Ohama</city>"
strip_city = s.lstrip("<city>").rstrip("</city>")
print(
strip_city) 
[Image: npafFhp.gif]
(Dec 8, 2016 05:29 PM)Retribute Wrote:  So you just want the city name without the tags?

PHP Code

<?php 
city 
= "<city>Ohama</city>"
strip_city = s.lstrip("<city>").rstrip("</city>")
print(
strip_city) 
Can't just do that because there is also other info like <age>, <name>, etc
You could use a regular expression. Assuming, of course, you know what the input is going to be. Otherwise, you'd need a much more complex one, and I'm AWFUL with them. B3mb4m will be able to write you a nicer one.

PHP Code

<?php 
tag 
= re.compile(r"\<(\S+)\>(.*)</\1>")
for 
group in tag.findall("<city>Ohama</city><age>100</age><name>Goodies</name>"):
    
tag, value = group
    
print("{}: {}".format(tag, value)) 

Output:
Code
city: Ohama
age: 100
name: Goodies
SRPP Discord Server

I am a professional network/cloud security architect.
My Old Tutorials
(Dec 8, 2016 05:49 PM)Goodies Wrote:  You could use a regular expression. Assuming, of course, you know what the input is going to be. Otherwise, you'd need a much more complex one, and I'm AWFUL with them. B3mb4m will be able to write you a nicer one.

PHP Code

<?php 
tag 
= re.compile(r"\<(\S+)\>(.*)</\1>")
for 
group in tag.findall("<city>Ohama</city><age>100</age><name>Goodies</name>"):
    
tag, value = group
    
print("{}: {}".format(tag, value)) 

Output:
Code
city: Ohama
age: 100
name: Goodies
It's supposed to only use strip or split
(Dec 8, 2016 05:58 PM)Cash Wrote:  It's supposed to only use strip or split

Yuck. I hate constraints.

PHP Code

<?php 
data 
= "<name>Goodies</name>"
_ = data.lstrip("<").rstrip(">")
tag, _ = _.split(">")
value = _.split("<")[0]
print(
tag, value) 

This is an absolutely ridiculous, ugly, and unsafe way of doing things, but whatever...
SRPP Discord Server

I am a professional network/cloud security architect.
My Old Tutorials


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Python] by Bass_ReevesAug 20, 2023 01:44 AM 9 632 Aug 20, 2023 01:44 AM
Last Post: Lucky4L
[.NET] by p3t3yxxnessJan 21, 2018 01:23 PM 4 156 Jan 21, 2018 01:23 PM
Last Post: VmX5
[C#] by forgotOldAccountCredsOct 1, 2016 10:19 AM 1 296 Oct 1, 2016 10:19 AM
Last Post: Ninja