Facebook API:抓取问题以及未知的希伯来语Unicode

2024-05-19 13:33:03 发布

您现在位置:Python中文网/ 问答频道 /正文

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

问题是,当我收到通知时,我没有很好地处理它:

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

正如您所看到的,“{u'title_text':u'…“}”不属于那里。 我怎样才能只收到里面的短信

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

{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

Tags: of字符串texttitleisunicodenotificationutf
1条回答
网友
1楼 · 发布于 2024-05-19 13:33:03

{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
⌤

相关问题 更多 >