一个Python初学者。 我试图找出如何编写一个方法,在搜索歌曲标题或对列表排序后获取“link”值,并让它播放。比如说,我会输入“Hurt”这个词,它会告诉我找到了。我遇到的问题是试图获得它的链接并打开Youtube网站。Json文件的加载非常好。我只需要找出搜索后的下一步。你知道吗
Json文件是:
{"LinkCollection":
[{"title":"I Will Always Love You" ,
"artist":"Whitney Houston" ,
"link":"http://www.youtube.com/watch?v=3JWTaaS7LdU",
"id":1},
{"title":"Killing Me Softly" ,
"artist":"Roberta Flack" ,
"link":"http://www.youtube.com/watch?v=LQ2t5e7stVM",
"id":2},
{"title":"Hero" ,
"artist":"Mariah Carey" ,
"link":"http://www.youtube.com/watch?v=0IA3ZvCkRkQ",
"id":3},
{"title":"Hurt" ,
"artist":"Christina Aguliera" ,
"link":"http://www.youtube.com/watch?v=wwCykGDEp7M",
"id":4},
{"title":"At Last" ,
"artist":"Etta James" ,
"link":"http://www.youtube.com/watch?v=S-cbOl96RFM",
"id":5}
]}
课程是:
from pprint import pprint
import json
import operator
from operator import itemgetter
import webbrowser
dataFile = "Music_Database.json"
class MusicLink():
def __init__(self):
print "Karaoke"
def loadData(self,dataFile):
musicObject = []
json_data=open(dataFile)
musicObject = json.load(json_data)
json_data.close()
return musicObject
def searchSong(self,search):
foundList = []
musicObject=[]
musicObject =self.loadData(dataFile)
#pprint(musicObject["LinkCollection"])
howmanySongs = len(musicObject["LinkCollection"])
print "You have %s stored online" % howmanySongs
for counter in range(howmanySongs):
print musicObject["LinkCollection"][counter]["title"]
name = musicObject["LinkCollection"][counter]["title"].lower()
print " ." # show this when you start a new record
lowerCaseSearch = search.lower()
didIfindIt = name.find( lowerCaseSearch)
print didIfindIt
if didIfindIt >= 0 :
print "*" # show this when you find it
foundList.append( musicObject["LinkCollection"][counter])
return foundList
def sortByTitle(self,foundMedia):
sortedTitleList = []
sortedTitleList = sorted(foundMedia, key=itemgetter('title'))
return sortedTitleList
def sortByArtist(self,foundMedia):
print"here"
pprint(foundMedia)
sortedByArtistList = []
sortedByArtistList = sorted(foundMedia, key=lambda song: (song['artist']), reverse=True)
return sortedByArtistList
def displayUrl(self,newSong):
#can't figure out the next step
return ""
def playSong(self, url):
webbrowser.open_new_tab(url)
return ""
主文件是:
search = raw_input("Find this Song: ")
results= m.searchSong(search)
pprint(results)
sortedResultByTitle = m.sortByTitle(results)
print "Sorted by Title"
pprint(sortedResultByTitle)
那可能是你的主要档案
我忍不住修改了你的一个方法。输出较少,但我认为这是出于调试目的。你知道吗
在主要的编辑之后,我添加了整个类的实现
你不知道如何设计一个类,尤其是方法。这就是我对整件事的看法。你知道吗
这就是如何使用它:
相关问题 更多 >
编程相关推荐