0
我有這樣的代碼:如何將已定義的變量和值放入Python字典?
if 'instagramTV' in path:
self.send_response(200)
instaShortcode, LTV, EMAIL, TIME, userID, URL = map(\
qs.get, ['instaID', 'channelID', 'uEmail', 'Time', 'uID', 'contentUrl'])
channelID, uEmail, instagramShortcode, uTime, uID, contentUrl = map(\
lambda x : str(x)[2:-2], [LTV, EMAIL, instaShortcode, TIME, userID, URL])
for i in (channelID, uEmail, instagramShortcode, uTime, uID, contentUrl):
print i
instaSTEP2 = requests.get("http://api.instagram.com/oembed?url=http://instagr.am/p/%s/"% instagramShortcode).json()
instaMeID = instaSTEP2['media_id']
instaINFO = requests.get("https://api.instagram.com/v1/media/%s?accesstoken=295391286.1b882b8.33fa51373fae4885b5c60ceb186e6560" % instaMeID).json()
print instaINFO['data']['user']['profile_picture']
print instaINFO['data']['user']['username']
print instaINFO['data']['caption']['text']
print instaINFO['data']['images']['standard_resolution']['url']
ltvMSG = {'fromEMAIL': 'uEmail', 'toCHANNELID': 'channelID', 'timeSENT': 'uTime', 'profiePIC': "instaINFO['data']['user']['profile_picture']",'userNAME': "instaINFO['data']['user']['username']", 'msgBODY': "instaINFO['data']['caption']['text']", 'msgIMAGE': "instaINFO['data']['images']['standard_resolution']['url']"}
print ltvMSG
首先瓦爾從HTTP進來的GET請求,然後我用其中的一些增值經銷商,使一個API調用,然後我找回一些JSON。
我試圖把get請求中的一些初始變量和api調用中的一些值放入我自己的dict/json中,這些值最終會進入redis列表。
打印ltvMSG返回此:
{'userNAME': "instaINFO['data']['user']['username']", 'timeSENT': 'uTime', 'msgIMAGE': "instaINFO['data']['images']['standard_resolution']['url']", 'msgBODY': "instaINFO['data']['caption']['text']", 'fromEMAIL': 'uEmail', 'toCHANNELID': 'channelID', 'profilePIC': "instaINFO['data']['user']['profile_picture']"}
這是我想要的結構,但如何使其真正值顯示爲值的鍵。
謝謝。在你的知識中是否有更高效的方式來完成我的目標?指出我可能研究的方向。再次感謝! – sirvon
我不完全確定你要在這裏做什麼,所以我很難幫助你實現它。我也不太瞭解Instagram API返回的數據結構,因此我不能推薦使用該數據的不同方式。爲了提高效率,網絡開銷似乎會成爲瓶頸,所以我不確定你應該爲程序的效率擔心。考慮代碼的清晰度可能更值得你花時間。但是,如果程序運行緩慢,請查看您的數據結構和操作,並查看可以進行更改的位置。 – pswaminathan
謝謝....什麼可以區分清楚的代碼不是? – sirvon