How to put certain parts of a string into a list
So i have the code
op = urlopen('http://example.com/download_music/' + mus + '-1.html')
ops = op.read()
af = file('mus.txt', 'w')
asv = file('mu.txt', 'w')
af.write(str(ops))
if 'charset="utf-8">var playlist' in ops:
print 'yes'
cal = ops.split('charset="utf-8">var playlist',1)
del cal[0]
asv.write(str(cal))
cv = str(cal)
cals = cv.split('},];</script><div',1)
del cals[1]
cals = str(cals)
v = cals.replace('{', '''
''')
vn = v.replace('[','')
vnm = vn.replace(']','')
print vnm
Then i have the author,the title, type of file and id of the song and i seperated each song with \n.
So after each newline i want to put each of the text into a list.
Also if you see any where in my code that i could improve please tell me.
and the string i want to seperate is:
'\' =
author : "Noah and the whale",title : "Lifegoeson",type : "sound",file : "http://www.themusiciv.com/wp-content/uploads/2011/02/L.I.F.E.G.O.E.S.O.N..mp3",id : "efca2792fc0dc9c076e5ef96c6b88d62" },
author : "Noah and the whale",title : "Lifegoeson",type : "sound",file : "http://www.maestrobilly.com/SFTMCHN/natw_lgo.mp3",id : "8da985a2a5757f3e54c20341a01a2a3e" },
author : "Noah and the whale",title : "Stranger",type : "sound",file : "http://www.tittletunes.com/wp-content/uploads/08%20Stranger.mp3",id : "473f4056c8ca5afb61677b86cda7ce82" },
author : "Noah and the whale",title : "Mary",type : "sound",file : "http://veradio.com/sounds/Noah%20And%20The%20Whale%20-%20Peaceful--%5b2008%5d%5bCD+SkidVid_XviD+Cov%5d/10%20%20Noah%20And%20The%20Whale%20-%20Mary.mp3",id : "824ef3d3d87088cebc18205cf02187f3" },
author : "Noah and the whale",title : "Lifegoeson",type : "sound",file : "http://www.directcurrentmusic.com/storage/mp3s-11/noah%20%20the%20whale%20-%20l.i.f.e.g.o.e.s.o.n..mp3",id : "92765bf124a4575950293ac24181daed" },
author : "Noah and the whale",title : "Jocasta",type : "sound",file : "http://veradio.com/sounds/Noah%20And%20The%20Whale%20-%20Peaceful--%5b2008%5d%5bCD+SkidVid_XviD+Cov%5d/02%20%20Noah%20And%20The%20Whale%20-%20Jocasta.mp3",id : "230b4eaea4bc076e4aa71e0722dfd294" },
author : "Noah and the whale",title : "At开发者_JS百科oms in a molecule",type : "sound",file : "http://veradio.com/sounds/Noah%20And%20The%20Whale%20-%20Peaceful--%5b2008%5d%5bCD+SkidVid_XviD+Cov%5d/01%20%20Noah%20And%20The%20Whale%20-%202%20Atoms%20In%20A%20Molecule.mp3",id : "1b078ccba9a0cd060723a6aeb6f475f7" },
author : "Noah and the whale",title : "Rocks and daggers",type : "sound",file : "http://veradio.com/sounds/Noah%20And%20The%20Whale%20-%20Peaceful--%5b2008%5d%5bCD+SkidVid_XviD+Cov%5d/08%20%20Noah%20And%20The%20Whale%20-%20Rocks%20And%20Daggers.mp3",id : "3b8b6bb05150b29dadcd183cb6ff8f48" },
author : "Noah and the whale",title : "Rocks and daggers",type : "sound",file : "http://earitnow.com/uploads/mp3s/noahandthewhale/05-noah_and_the_whale-rocks_and_daggers.mp3",id : "b67153c57290f324576ec0aca73e8f32" },
author : "Noah and the whale",title : "Mary",type : "sound",file : "http://www.rcdc.it/audio/maps/Audio//Noah And The Whale - Mary.mp3",id : "6428201fca47fa66a902fc6779992e2e" '
A quick hack would be something like
# x is your string
import re
parsed = re.findall('author : "([^"]+)",title : "([^"]+)",type : "([^"]+)",file : "([^"]+)",id : "([^"]+)', x)
print parsed
精彩评论