wechat_sender icon indicating copy to clipboard operation
wechat_sender copied to clipboard

python2.7环境下中文微信昵称或者群组报编码错误

Open lyp2008001 opened this issue 8 years ago • 13 comments

group = bot.groups().search('游否')[0] File "C:\Python27\lib\site-packages\wxpy\api\chats\groups.py", line 71, in search return Groups(filter(match, self)) File "C:\Python27\lib\site-packages\wxpy\api\chats\groups.py", line 61, in match if not match_name(group, keywords): File "C:\Python27\lib\site-packages\wxpy\utils\misc.py", line 185, in match_name if kw in str(getattr(chat, attr, '')).lower(): UnicodeDecodeError: 'utf8' codec can't decode byte 0xe6 in position 0: unexpected end of data

lyp2008001 avatar Jun 12 '17 06:06 lyp2008001

方便提供下操作系统版本嘛? @lyp2008001

bluedazzle avatar Jun 12 '17 12:06 bluedazzle

@bluedazzle win10企业版

lyp2008001 avatar Jun 13 '17 01:06 lyp2008001

请问问题解决了吗。好像也碰到了这个问题

leemon9527 avatar Jun 14 '17 01:06 leemon9527

看起来是 wxpy Windows 下编码的问题,会尽快修复。到时候可能需要重新安装 wxpy 库

bluedazzle avatar Jun 14 '17 02:06 bluedazzle

刚才研究了下 misc.py 中引入了future.builtins的str if PY2: from future.builtins import str 方法prepare_keywords中的参数如果使用python2.7的默认的str会导致判断isinstance(keyword,str)不成立

导致最后返回的map结果出错。

我暂时的解决办法是 在微信发送端的脚本中同样引入 from future.builtins import str ,然后在search的时候强制下类型转换 示例

from wxpy.compatible import PY2 if PY2: from future.builtins import str

def main(): bot = Bot()

my = bot.friends().search(str('蓦然'))
listen(bot,receivers=my)

leemon9527 avatar Jun 14 '17 02:06 leemon9527

嗯嗯,Windows 下系统编码不是 utf-8 所以 future.builtins 的 str 会出错。你可以先那样解决,我在 wxpy develop 分支修复了这个问题。你可以安装这个分支的 wxpy 试试 @leemon9527

bluedazzle avatar Jun 14 '17 02:06 bluedazzle

@bluedazzle 刚才重新安装了wxpy的develop分支,好像还会报错 File "D:/Seafile/pyproject/PycharmTestProjects/wechat_msg_test/wx_msg.py", line 19, in main() File "D:/Seafile/pyproject/PycharmTestProjects/wechat_msg_test/wx_msg.py", line 14, in main my = bot.friends().search('蓦然') File "C:\Python27_10\lib\site-packages\wxpy-0.3.9.7-py2.7.egg\wxpy\api\chats\chats.py", line 50, in search return Chats(filter(match, self), self.source) File "C:\Python27_10\lib\site-packages\wxpy-0.3.9.7-py2.7.egg\wxpy\api\chats\chats.py", line 44, in match if not match_name(chat, keywords): File "C:\Python27_10\lib\site-packages\wxpy-0.3.9.7-py2.7.egg\wxpy\utils\misc.py", line 187, in match_name if kw in '{0}'.format(getattr(chat, attr, '')).lower(): UnicodeDecodeError: 'utf8' codec can't decode byte 0xe8 in position 0: unexpected end of data

leemon9527 avatar Jun 14 '17 02:06 leemon9527

ok,我再看看

bluedazzle avatar Jun 14 '17 03:06 bluedazzle

group = bot.groups().search(u'游否')[0]

'游否' 前面 加一个 u --> u'游否'

解决了

slyang-git avatar Oct 27 '17 09:10 slyang-git

Traceback (most recent call last): File "C:\Program Files\JetBrains\PyCharm 2018.1.2\helpers\pydev\pydevd.py", line 1664, in main() File "C:\Program Files\JetBrains\PyCharm 2018.1.2\helpers\pydev\pydevd.py", line 1658, in main globals = debugger.run(setup['file'], None, None, is_module) File "C:\Program Files\JetBrains\PyCharm 2018.1.2\helpers\pydev\pydevd.py", line 1068, in run pydev_imports.execfile(file, globals, locals) # execute the script File "C:\Program Files\JetBrains\PyCharm 2018.1.2\helpers\pydev_pydev_imps_pydev_execfile.py", line 18, in execfile exec(compile(contents+"\n", file, 'exec'), glob, loc) File "C:/developer/python_workplace/Python/wechat_sender_out.py", line 1, in from wechat_sender import Sender File "C:\developer\python_workplace\Python\wechat_sender.py", line 4, in listen(bot) NameError: name 'listen' is not defined

JustDoItQz avatar May 15 '18 08:05 JustDoItQz

@bluedazzle 该怎么解决?

JustDoItQz avatar May 15 '18 08:05 JustDoItQz

我下载的最新版

JustDoItQz avatar May 15 '18 08:05 JustDoItQz

@JustDoItQz 不要把你的文件名命名为 wechat_sender.py 换个名字

bluedazzle avatar May 20 '18 02:05 bluedazzle