如何调用一个特定的、未知的Python对象属性?
我正在努力创建一个简单的 Python 脚本,它最终会告诉您在给定月份内发布了多少博客条目,并且 pyblog 应用程序被证明非常有帮助。
但是,当我创建博客对象时,我不知道如何访问它的各种属性。我可以通过打印字典中的一项来将它们全部打印出来,如下所示(摘录):
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}]
但是我怎样才能让它提供 DateTime 信息呢?
I'm working to create a simple Python script that will ultimately tell you how many blog entries were posted in a given month, and the pyblog app is proving very helpful.
However, when I create the blog object, I don't know how to access it's various attributes. I can print them all out by printing one item from the dictionary, as shown (in excerpts) below:
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}]
But how can I just get it to provide that DateTime information?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果
blog.get_recent_posts(1)
是一本字典,print
将显示前大括号和尾大括号,而您所显示的内容从不知名的地方开始,以另一个大括号结束荒无人烟的地方——绝对荒谬。我假设你做了一些奇怪的复制和过去的错误,并且这个东西是一本字典。在这种情况下,获取例如创建日期很容易,只需一个索引:
编辑:OP编辑了Q以显示整个结果(并提到了一些有用的错误消息),这使得清除该函数的结果是一个包含一本字典的列表。鉴于这种情况,正确的说法是:
If
blog.get_recent_posts(1)
was a dictionary, theprint
would show leading and trailing braces, while what you're showing starts in the middle of nowhere and ends in another middle of nowhere -- absolutely absurd.I'm going to assume you made some weird copy and past error and that the thing is a dictionary. In that case, getting for example the creation date is easy, just an indexing:
Edit: the OP edited the Q to show the whole result (and mentioned some helpful error messages) which, all together, makes clear the function's result is a list with one dictionary in it. Given that this is the case, the correct statement becomes: