Python UnicodeDecor导入错误(tweepy)

Python UnicodeDecor导入错误(tweepy),python,unicode,tweepy,Python,Unicode,Tweepy,我试图在tweepy中使用函数“update_profile_background_image”,但出现错误: Traceback (most recent call last): File "XXX.py", line 1401, in <module> psn_card.gen_twitter_bg(user_db) File "XXX.py", line 972, in gen_twitter_bg auth_api.update_profile_bac

我试图在tweepy中使用函数“update_profile_background_image”,但出现错误:

Traceback (most recent call last):
  File "XXX.py", line 1401, in <module>
    psn_card.gen_twitter_bg(user_db)
  File "XXX.py", line 972, in gen_twitter_bg
    auth_api.update_profile_background_image(file)
  File "build/bdist.linux-x86_64/egg/tweepy/api.py", line 346, in update_profile_background_image
    headers, post_data = API._pack_image(filename, 800)
  File "build/bdist.linux-x86_64/egg/tweepy/api.py", line 729, in _pack_image
    body = '\r\n'.join(body)
UnicodeDecodeError: 'ascii' codec can't decode byte 0x89 in position 0: ordinal not in range(128)

我猜
filename
是一个Unicode字符串。不幸的是,Tweepy不支持Unicode文件名。这是虫子吗?可以说

问题是它试图使用Unicode字符串逐字创建HTTP POST数据,而不是将其编码为字节字符串:

body.append('Content-Disposition: form-data; name="image"; filename="%s"' % filename)
这将使
正文
列表中的一个字符串成为Unicode字符串,当序列中的一个字符串是Unicode字符串时,您尝试
join()
它们,结果将是Unicode。但是,HTTP POST正文是一个字节字符串,其中包含大量二进制代码,因此它与ASCII不兼容,因此尝试隐式强制它使用Unicode失败


(在任何情况下,
Content-Disposition
中给出的文件名肯定不应该像上面的代码那样包含完整的路径。我建议使用类似于
filename=os.path.basename(filename.encode('us-ascii','ignore')的方法)
作为第一个快速修复方法出现在上面的前一行。我不确定Twitter是否关心文件名是什么,但是…

我猜
文件名是一个Unicode字符串。不幸的是,Tweepy不支持Unicode文件名。这是虫子吗?可以说

问题是它试图使用Unicode字符串逐字创建HTTP POST数据,而不是将其编码为字节字符串:

body.append('Content-Disposition: form-data; name="image"; filename="%s"' % filename)
这将使
正文
列表中的一个字符串成为Unicode字符串,当序列中的一个字符串是Unicode字符串时,您尝试
join()
它们,结果将是Unicode。但是,HTTP POST正文是一个字节字符串,其中包含大量二进制代码,因此它与ASCII不兼容,因此尝试隐式强制它使用Unicode失败


(在任何情况下,
Content-Disposition
中给出的文件名肯定不应该像上面的代码那样包含完整的路径。我建议使用类似于
filename=os.path.basename(filename.encode('us-ascii','ignore')的方法)
作为第一个快速解决方案,我不确定Twitter是否关心文件名是什么,但是…

请提供更多信息。你对update\u profile\u background\u image()的调用是什么样子的?错误可能在那里,我们还不能确定。可能有用的一般链接:请提供更多信息。你对update\u profile\u background\u image()的调用是什么样子的?错误可能存在,我们还无法判断。可能有用的常规链接: