Facebook API:报废问题以及未知的希伯来语Unicode

时间:2014-03-25 06:50:51

标签: python facebook api unicode

我创建了一个与我的python脚本集成的Facebook应用程序。 我有权从用户处获取通知和消息。

问题是当我收到通知时,我不会废弃它:

notification of Somebody is {u'title_text': u'Texas HoldEm Poker: Hurry! CLAIM your 
$25,000 FREE CHIPS now!'}

如你所见,“{u'title_text':你'......”}“不属于那里。 我怎样才能获得里面的短信?

第二个问题是当我试图用希伯来语传递信息时,它看起来像这样:

{u'title_text': u'\u200e\u200e\u05d7\u05df \u05d1\u05dc\u05d7\u05e0\u05e1\u200e posted 
on Danie's\'s timeline\u200e: "Have a lot of good luck"'}

“\ u200e \ u200e \ u05d7 \ u05df \ u05d1 \ u05dc \ u05d7 \ u05e0 \ u05e1 \ u200e”是希伯来语中的某个人的名字,我如何将其编码为完美的名称本身?

谢谢。

编辑:我发现unicode是“utf-8”,我需要在字符串前添加“u” 但是如果我的程序得到一个字符串怎么办...如何在现有字符串中添加“u”? 感谢。

编辑:更新代码:

def insertNewNotification(notification_list, owner):

for notification in notification_list:
    notification = repr(notification['title_text'])
    notification = str(notification)
    notification = unicode(notification, 'unicode-escape')
    notification = notification.encode("UTF-8").decode("UTF-8")
    print "notification of " + owner + " is " + notification
    response = json.load(urllib.urlopen((url + "add_notification&message=" + notification + "&owner=" + owner).encode("UTF-8")))
return 1

1 个答案:

答案 0 :(得分:0)

{u'title_text': u'Texas HoldEm Poker: Hurry! CLAIM your $25,000 FREE CHIPS now!'}是具有Unicode键和值的字典的表示(repr())。

u""是Python中的Unicode文字。您只能在Python源代码中键入u

您可能在print "notification of Somebody is", data

的某处有以下代码

您应该使用print data['title_text']代替。

了解之间的区别:

>>> s = u"\u2324"
>>> s
u'\u2324'
>>> s.encode('utf-8')
'\xe2\x8c\xa4'
>>> print repr(s)
u'\u2324'
>>> print s
⌤