我在Python上使用Json搞了一點,我想讀tvmaze.com提供的API。雖然我可以得到項目很好,但名單很長,我想限制它從我的本地時鐘+1小時。例如,如果我的計算機時鐘是15:00,以便在16:30到15:30之間向我顯示這些項目,則會在16:30之前向我顯示這些項目。在PHP中,我會添加3600秒到當前時間,但在Python中,整個時間三角洲似乎令人困惑。來自Feed的Python打印項目匹配當前時間1小時後
這是我目前使用的
import urllib2
import simplejson
response = urllib2.urlopen("http://api.tvmaze.com/schedule?country=US&date=2017-07-17")
data = simplejson.load(response)
myList=["abc","amc","animal planet","bravo","cartoon network","cbs","comedy central","the cw","discovery channel","fox","freeform","hbo","history","mtv","nbc","nickelodeon","tbs","tnt","usa network","wwe network"]
for post in data:
if post["show"]["network"]["name"].lower() in myList:
airtime = post["airtime"]
network = post["show"]["network"]["name"]
name = post["show"]["name"]
season = post["season"]
ep = post["number"]
time = post["show"]["schedule"]["time"]
date = post["airdate"]
#summary = ["show"]["summary"]
print airtime,"",name,"- Season",season,"EP",ep,"("+network+")"
樣本輸出的Python代碼:
21:00 Preacher - Season 2 EP 5 (AMC)
21:00 Will - Season 1 EP 3 (TNT)
21:00 American Pickers - Season 17 EP 10 (History)
21:00 Stitchers - Season 3 EP 6 (FreeForm)
21:00 Superhuman - Season 1 EP 6 (FOX)
21:00 Whose Line Is It Anyway? - Season 13 EP 6 (The CW)
21:00 Teen Mom 2 - Season 8 EP 1 (MTV)
21:00 Street Outlaws: New Orleans - Season 2 EP 4 (Discovery Channel)
21:00 The Real Housewives of Orange County - Season 12 EP 2 (Bravo)
21:00 Teen Mom - Season 7 EP 30 (MTV)
21:00 Alaska: The Last Frontier: The Frozen Edge - Season 4 EP 13 (Animal Planet)
我顯然做錯了,因爲我得到一個錯誤ValueError:需要更多超過1個值才能解包。 – Slightz
比它意味着某些'post [「通話時間」]不包含有效時間,請檢查更新。 – zwer
我將如何實現與我的代碼?在for循環中添加if和elif? – Slightz