tkinter.TclError:字符U + 1f449高于Tcl允许的范围(U + 0000-U + FFFF)

时间:2017-09-12 10:02:56

标签: python twitter tkinter tweepy

我想用tweepy在Tkinter窗口上显示我的推特时间线。这是代码

import tweepy
import tkinter

consumer_key = 'xxxxxxxxxxxxxx'
consumer_sec ='xxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
acc_token = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
acc_token_sec = 'xxxxxxxxxxxxxxxxxxxxxx'
auth = tweepy.OAuthHandler(consumer_key,consumer_sec)
auth.set_access_token(acc_token,acc_token_sec)

api = tweepy.API(auth)

tweets = api.home_timeline()

tkwindow = tkinter.Tk()

for tweet in tweets:
    i = 1
    label = tkinter.Label(tkwindow, text=tweet.author.name + " " + str(tweet.created_at) + "\n" + str(tweet.text))
    if i == 5:
        break
tkwindow.mainloop()

但我有以下错误

  

_tkinter.TclError:字符U + 1f449高于Tcl允许的范围(U + 0000-U + FFFF)

据我所知,tkinter无法显示真实推文中出现的一些特殊图标,但实际上,我不想展示那些,我只是想显示推文的简单文字,

那么如何避免此错误并仅显示推文的文本

2 个答案:

答案 0 :(得分:1)

最简单的方法是删除多余的字符。这可以在for循环开始时使用以下代码完成:

char_list = [tweet[j] for j in range(len(tweet)) if ord(tweet[j]) in range(65536)]
tweet=''
for j in char_list:
    tweet=tweet+j

答案 1 :(得分:0)

问题在 python 错误报告中描述:

我把发布在那里的解决方案调整为处理 gmail 邮件,其中主题行有时会以某种方式插入不正确的字符:

try:            
    iid = self.lib_tree.insert("", tk.END, tags="unchecked", 
        values=(message_id, subject, sender, epoch, date, labels,
                snippet))
except tk.TclError:
    print('old subject:', subject)
    
    import re

    # From: https://bugs.python.org/issue21084
    astral = re.compile(r'([^\x00-\uffff])')
    s = subject
    new_subject = ""
    for i, ss in enumerate(re.split(astral, s)):
        if not i%2:
            new_subject += ss
        else:
            new_subject += '?'
    print('new_subject:', new_subject)
    continue

加载大约 5,000 封 Gmail 邮件的程序输出:

$ bserve.py
old subject: Mserve June 18/2021
new_subject: Mser?e June 18/2021
old subject: mserve December 7, 2020
new_subject: mser?e December 7, 2020
old subject: Re: WinEunuuchs2Unix
new_subject: Re: WinEunuuchs2Uni?
old subject: ? ORDER UPDATE: 2019 Cigarette Light...
new_subject: ? ORDER UPDATE: 2019 Cigarette Light...
old subject: ? You've got 18 new ideas waiting for you!
new_subject: ? You'?e got 18 ne? ideas ?aiting for ?ou!
old subject: ? Get inspired with these 18 trending ideas
new_subject: ? Get inspired ?ith these 18 trending ideas
old subject: ? 18 popular Pins for you
new_subject: ? 18 popular Pins for ?ou
old subject: ? Get inspired with these 18 trending ideas
new_subject: ? Get inspired ?ith these 18 trending ideas
old subject: ? Get inspired with these 18 trending ideas
new_subject: ? Get inspired ?ith these 18 trending ideas
old subject: ? You've got 18 new ideas waiting for you!
new_subject: ? You'?e got 18 ne? ideas ?aiting for ?ou!
old subject: ? We picked some Puzzle art Pins for you
new_subject: ? We picked some Pu??le art Pins for ?ou
old subject: ? INFANTRY Mens LCD Digital Quartz Wrist Watch Tactical Sport Stainless Steel Navy is HOT on eBay, but quantity is limited
new_subject: ? INFANTRY Mens LCD Digital Quart? Wrist Watch Tactical Sport Stainless Steel Na?? is HOT on eBa?, but quantit? is limited
old subject: ? POLJOT AVIATOR ATTACK PLANE MEN`S  RUSSIAN MILITARY WATCH is HOT on eBay, but quantity is limited
new_subject: ? POLJOT AVIATOR ATTACK PLANE MEN`S  RUSSIAN MILITARY WATCH is HOT on eBa?, but quantit? is limited

有些是显而易见的原因,例如“?”。有些不太明显,例如“Mserve”的“v”无效并变成“Mser?e”。

我想是我在旅行结束时回家时在 Android 中写了我的 mserve 项目电子邮件时出现了故障。