Question

I get this string via Python, using a request to a Shoutcast server:

BEST SHOW EVER http://www.myradio.foo/xml/logos/showlogo.jpg Avicii Hey Brother

And I'd like to get a dict containing this:

mystring[showtitle] = 'BEST SHOW EVER' mystring[image] = 'http://www.myradio.foo/xml/logos/showlogo.jpg' mystring[song] = 'Avicii hey brother'

The string is always ASCII, and anything could be written after and before the link.

How can i parse that? I guess I need to use regex, but i heard they aren't very fast.


Also, the title needs a bit of tweaking, to look prettier.

Avicii hey brother

Becomes

Avicii - Hey Brother

What would you raccomend to do that? I thought about searching the title on iTunes and get from the first result all the data, but I'm not sure how to do that (all links for the iTunes API redirect me to the Apple SDK, that i would like to not use).

Était-ce utile?

La solution 2

I was wrong about the dict() Here is the updated solution that converts it into dict.

response = '''BEST SHOW EVER http://www.myradio.foo/xml/logos/showlogo.jpg Avicii Hey Brother'''

## parsing using named group
m = re.match("(?P<showtitle>.*?)\s+(?P<image>https?://\S+)\s+(?P<song>.+)", response);
mystring = m.groupdict()
print mystring['song']

You can not always convert the song name on your format always. Cause you don't know which one is song name, or which one album name. If its always fixed that the first word is the album name, then you can do this one:

print re.sub("^(\S+)\s", "\\1 - ", mystring['song'])

Autres conseils

Right, so you can get the dictionary as specified like this:

>>> s = 'BEST SHOW EVER http://www.myradio.foo/xml/logos/showlogo.jpg Avicii hey brother'
>>> mystring = dict(zip(('showtitle', 'image', 'song'), re.search('(.+) (http:.+?) (.+)', s).groups()))
>>> mystring
{'image': 'http://www.myradio.foo/xml/logos/showlogo.jpg', 'showtitle': 'BEST SHOW EVER', 'song': 'Avicii Hey Brother'}

Then you can 'prettify' the song item by doing this:

>>> mystring[song] = out[song].title()
>>> mystring[song]
'Avicii Hey Brother'
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top