2010-06-03 83 views
0

我正在努力創建一個簡單的Python腳本,它將最終告訴您在給定的月份中發佈了多少博客條目,並且pyblog應用程序證明非常有用。如何調用特定的未知Python對象屬性?

但是,當我創建博客對象時,我不知道如何訪問它的各種屬性。我可以從字典中打印一個項目全部打印出來,如圖所示(節選中)所示:

print blog.get_recent_posts(1) 

[{'mt_keywords': 'Storage in 2010, Storage, Twitter', 'permaLink': 'http://itknowledgeexchange.techtarget.com/IT-watch-blog/cream-of-the-twitter-crop-storage-edition/', 'wp_slug': 'cream-of-the-twitter-crop-storage-edition', 'description': '<a href="http://http.cdnlayer.com/itke/blogs.dir/141/files/2010/06/follow.jpg"><img class="alignright size-medium wp-image-1148" style="margin: 4px" src="http://http.cdnlayer.com/itke/blogs.dir/141/files/2010/06/follow.jpg" alt="" width="279" height="157" /></a>Here they are, this month\'s specialized top 10 Twitter accounts. From IT pros with impressive storage insight to the industry greats sending out updates on their company\'s innovations, check out these folks on Twitter for daily bites of what you need to know about storage.\n<ul>\n\t<li><a href="http://twitter.com/3parfarley" target="_blank">3parfarley</a>: Marc Farley, the blogger behind StorageRap, sends out bite-sized bits of storage info. He\'s also one of <a title="Marc Farley" href="http://searchstorage.techtarget.com/expert/KnowledgebaseBio/0,289623,sid5_cid382505,00.html" target="_blank">SearchStorage.com\'s experts</a>.</li>\n\t<li><a href="http://twitter.com/storagenerve" target="_blank">StorageNerve</a>: Devang Panchigar is in the storage/virtualization/computer industry; he blogs over at StorageNerve and Gestalt IT.</li>\n\t<li><a href="http://twitter.com/skenniston" target="_blank">skenniston</a>: Follow the Storage Alchemist himself.</li>\n\t<li><a href="http://twitter.com/storageanarchy" target="_blank">StorageAnarchy</a>: From the blogger behind Storage Anarchy, get some storage rebel yells in 140 characters or less.</li><li>', 'title': 'Cream of the Twitter Crop: Storage Edition', 'post_status': 'publish', 'date_created_gmt': <DateTime '20100601T19:27:17' at 2853530>, 'mt_excerpt': '', 'userid': '288', 'dateCreated': <DateTime '20100601T14:27:17' at 28533a0>, 'custom_fields': [{'value': '', 'id': '1317', 'key': 'brightcove_code'}, {'value': 'http://bit.ly/d0Rywl', 'id': '1403', 'key': 'topsy_short_url'}, {'value': '1', 'id': '1319', 'key': '_edit_last'}, {'value': '1275511812', 'id': '1318', 'key': '_edit_lock'}, {'value': 'http://itknowledgeexchange.techtarget.com/IT-watch-blog/cream-of-the-twitter-crop-storage-edition/', 'id': '1402', 'key': '_topsy_long_url'}], 'wp_author_display_name': 'Melanie Yarbrough', 'link': 'http://itknowledgeexchange.techtarget.com/IT-watch-blog/cream-of-the-twitter-crop-storage-edition/', 'mt_text_more': u'<a href="http://twitter.com/storagetexan" target="_blank">StorageTexan</a>: He\'s got 15 years in the IT industry, 10 of them spent - proudly - as a storage geek.</li>\n\t<li><a href="http://twitter.com/storage_wonk" target="_blank">Storage_Wonk</a>: Xiotech\'s principle architect blogs over at Storage Wonk.</li>\n\t<li><a href="http://twitter.com/storageio" target="_blank">storageio</a>: Greg Schulz is an independent IT advisor and consultant, and he blogs over at the Storageio blog.</li>\n\t<li><a href="http://twitter.com/saveonstorage" target="_blank">SaveOnStorage</a>: If you\'re looking to lower your TCO, check out Hitachi Data Systems\' Twitter account dedicated to just that with sustainable storage investments. Vendor(ish).</li>\n\t<li><a href="http://twitter.com/seagate" target="_blank">Seagate</a>: The "world leader" in storage devices. Vendor(ish).</li>\n\t<li><a href="http://twitter.com/compellent" target="_blank">Compellent</a>: Compellent\'s official Twitter for all things enterprise storage. Vendor(ish).</li>\n</ul>\nAnd there\'s always us. <a href="http://twitter.com/itke" target="_blank">Follow ITKE on Twitter</a> for updates on the IT community and industry. Did we miss someone? Let me know at <a href="mailto:[email protected]" target="_blank">[email protected]</a> or at the <a href="http://itknowledgeexchange.techtarget.com/itanswers/" target="_blank">ITKE Community Forum</a>.\n\n<em>Melanie Yarbrough is the assistant community editor at\xa0<a title="http://ITKnowledgeExchange." href="http://itknowledgeexchange.com/" target="_blank">ITKnowledgeExchange.com</a>. Follow her on <a href="http://twitter.com/myarbrough" target="_blank">Twitter </a>or send her an email at <a href="mailto:[email protected]" target="_blank">[email protected]</a>.</em>', 'mt_allow_comments': 1, 'wp_password': '', 'postid': '1061', 'wp_author_id': '288', 'categories': ['Storage in 2010', 'Storage', 'Twitter'], 'mt_allow_pings': 1}] 

但我如何才能得到它提供了日期時間信息?

+1

'pprint'是你的朋友。 – 2010-06-03 03:37:03

回答

2

如果blog.get_recent_posts(1)是字典,print會顯示引導和尾隨花括號,而您展示的內容開始於無處不在,並在另一箇中間無處結尾 - 這絕對是荒謬的。

我打算假設你做了一些奇怪的副本和過去的錯誤,並且的字典。在這種情況下,越來越例如創建日期是容易的,只是一個索引:

print blog.get_recent_posts(1)['dateCreated'] 

編輯:在OP編輯的Q可顯示整個結果(和提到了一些有用的錯誤消息),這,一起,明確了函數的結果是一個列表,其中包含一個字典。鑑於這種情況,正確的說法變爲:

print blog.get_recent_posts(1)[0]['dateCreated'] 
+0

你說得對。我從中間複製並粘貼,以避免形成非常長而醜陋的粘貼。我用整個輸出,一本字典對它進行了更新。我嘗試了你的建議,但得到了以下錯誤: 「TypeError:列表索引必須是整數,而不是str」。我嘗試了一些整數,並收到錯誤「列表索引超出範圍」,即使值爲1。 感謝您的指點!這對我自己的調查非常有幫助。 – 2010-06-03 03:12:48

+0

啊哈,這不是一個字典,它是一個包含一個字典的列表 - 這真是不可能用**非常**部分輸出(!)來猜測。編輯答案,告訴你如何得到你想要的。 – 2010-06-03 05:10:21

相關問題