0

I'm new to python, this is currently printing correctly, though I want it to show a specific number from the print.

from facepy import GraphAPI

graph = GraphAPI("Facebook access token")
query = graph.fql('SELECT unread_count FROM mailbox_folder WHERE folder_id = 0')
print query

This prints out

{u'data': [{u'unread_count': 2}]}

though I'd like to only show the number 2.. I've tried countless methods which I've deleted them as I tried. Why does python prints with the u' behind the information being printed?

4

1 回答 1

0

query是字典列表的字典。

>>> k = {u'data': [{u'unread_count': 2}]}
>>> k['data'][0]['unread_count']
2

字典{'key': value} 键必须是字符串但值可以是任何东西,从整数到图像。任何有效的 Python 对象。

列表包含在内[],其元素是逗号分隔值。例如:

lst_ints = [1,2,3,4]
lst_strs = ['1','2']
lst_dict = [ {'key1': val1}, {'key1': val1} ]

在大多数编程语言中,要访问列表和数组,您可以从索引 0 开始。SOlst_ints[0]将是整数 1。

query有一个键,data所以我们首先访问它。然后我们看到一个列表,其中有一个元素,索引为 0。最后,列表包含 key unread_count

u只是意味着unicode字符串。

于 2013-04-24T01:16:51.730 回答