'UCS-2' 编解码器无法对位置 61-61 中的字符进行编码
'UCS-2' codec can't encode characters in position 61-61
当我 运行 我的 Python 代码和打印(项目)时,我得到以下错误:
UnicodeEncodeError: 'UCS-2' codec can't encode characters in position 61-61: Non-BMP character not supported in Tk
这是我的代码:
def getUserFollowers(self, usernameId, maxid = ''):
if maxid == '':
return self.SendRequest('friendships/'+ str(usernameId) +'/followers/?rank_token='+ self.rank_token,l=2)
else:
return self.SendRequest('friendships/'+ str(usernameId) +'/followers/?rank_token='+ self.rank_token + '&max_id='+ str(maxid))
def getTotalFollowers(self,usernameId):
followers = []
next_max_id = ''
while 1:
self.getUserFollowers(usernameId,next_max_id)
temp = self.LastJson
for item in temp["users"]:
print(item)
followers.append(item)
if temp["big_list"] == False:
return followers
next_max_id = temp["next_max_id"]
我该如何解决这个问题?
如果不知道 temp["users"]
的内容很难猜测,但错误表明它包含非 BMP unicode 字符,例如 emoji.
如果您尝试在 IDLE 中显示它,您会立即收到此类错误。重现的简单示例(在 Python 3.5 的 IDLE 上):
>>> t = "ab \U0001F600 cd"
>>> print(t)
Traceback (most recent call last):
File "<pyshell#5>", line 1, in <module>
print(t)
UnicodeEncodeError: 'UCS-2' codec can't encode characters in position 3-3: Non-BMP character not supported in Tk
(\U0001F600
表示unicode字符U+1F600笑脸)
确实是Tk不支持code大于FFFF的unicode字符导致的错误。一个简单的解决方法是将它们从字符串中过滤掉:
def BMP(s):
return "".join((i if ord(i) < 10000 else '\ufffd' for i in s))
'\ufffd'
是 unicode U+FFFD REPLACEMENT CHARACTER.
的 Python 表示
我的例子变成:
>>> t = "ab \U0001F600 cd"
>>> print(BMP(t))
ab � cd
因此您的代码将变为:
for item in temp["users"]:
print(BMP(item))
followers.append(item)
当我 运行 我的 Python 代码和打印(项目)时,我得到以下错误:
UnicodeEncodeError: 'UCS-2' codec can't encode characters in position 61-61: Non-BMP character not supported in Tk
这是我的代码:
def getUserFollowers(self, usernameId, maxid = ''):
if maxid == '':
return self.SendRequest('friendships/'+ str(usernameId) +'/followers/?rank_token='+ self.rank_token,l=2)
else:
return self.SendRequest('friendships/'+ str(usernameId) +'/followers/?rank_token='+ self.rank_token + '&max_id='+ str(maxid))
def getTotalFollowers(self,usernameId):
followers = []
next_max_id = ''
while 1:
self.getUserFollowers(usernameId,next_max_id)
temp = self.LastJson
for item in temp["users"]:
print(item)
followers.append(item)
if temp["big_list"] == False:
return followers
next_max_id = temp["next_max_id"]
我该如何解决这个问题?
如果不知道 temp["users"]
的内容很难猜测,但错误表明它包含非 BMP unicode 字符,例如 emoji.
如果您尝试在 IDLE 中显示它,您会立即收到此类错误。重现的简单示例(在 Python 3.5 的 IDLE 上):
>>> t = "ab \U0001F600 cd"
>>> print(t)
Traceback (most recent call last):
File "<pyshell#5>", line 1, in <module>
print(t)
UnicodeEncodeError: 'UCS-2' codec can't encode characters in position 3-3: Non-BMP character not supported in Tk
(\U0001F600
表示unicode字符U+1F600笑脸)
确实是Tk不支持code大于FFFF的unicode字符导致的错误。一个简单的解决方法是将它们从字符串中过滤掉:
def BMP(s):
return "".join((i if ord(i) < 10000 else '\ufffd' for i in s))
'\ufffd'
是 unicode U+FFFD REPLACEMENT CHARACTER.
我的例子变成:
>>> t = "ab \U0001F600 cd"
>>> print(BMP(t))
ab � cd
因此您的代码将变为:
for item in temp["users"]:
print(BMP(item))
followers.append(item)