DO NOT DEAL WITH THIS MEMBER
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)
DO NOT DEAL WITH THIS MEMBER
(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
Dec 8, 2016 05:49 PM
(This post was last modified: Dec 8, 2016 05:52 PM by Goodies.)
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
DO NOT DEAL WITH THIS MEMBER
(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...
|