I have a long string of which I am attempting to get return a string after it occurs after
another string. For example, I look for the string ‘zombiesattack’ in the string first, I
then look for the first place that the string named ‘title’ occurs and want to print the save the text in between ‘title’ and ‘/title’ to another variable named ‘titleOfVideo’. I am having some difficulty doing this. Any advice?
string stored in the variable named data
data= <updated>2012-10-10T19:20:55.000Z</updated>
<abc>zombiesattack</abc>
<category scheme="http://schemas.google.com/g/2005#kind" term="http://gdata.youtube.com/schemas/2007#video" />
<category scheme="http://gdata.youtube.com/schemas/2007/categories.cat" term="Sports" label="Sports" />
<title>NY Yankees: 6 Essential Pieces of Postseason Memorabilia</title>
I’d like to save ‘NY Yankees: 6 Essential Pieces of Postseason Memorabilia’ to the variable ‘titleOfVideo’.
starting_point = data.find('zombiesattack')
new_string = data[starting_point:]
title_point = new_string.find('<title>')
print new_string[:title_point]
titleOfVideo = new_string[title_point:20]
When I try this and print titleOfVideo, I get a bunch of return lines.
Use a XML parser instead, such as ElementTree: