我正在尝试开始使用Groupy编写Groupme机器人。
与此相关的文章很多,但是它们似乎在技术上更具挑战性,解决方案似乎效率不高,而且都没有解决我遇到的问题。
这是我到目前为止的代码:
import groupy
token = "{My Token}"
from groupy.client import Client
client = Client.from_token(token)
groups = list(client.groups.list_all())
for group in groups:
print(group)
首先,我不认为这是相关的,但是在日食中它警告了groupy是未使用的导入,即使在我看来并非如此。
更重要的是,关于我遇到的问题的奇怪之处在于,错误消息的更改没有对以上代码进行任何编辑。
这是我收到的错误消息: 在每个错误消息中,我都编辑了文件位置的开头...因为我很偏执
Traceback (most recent call last):
File "\Projects\Eclipse\eclipse-workspace\Gort's Cousin\groupme_bot.py", line 15, in <module>
<Group(name='One of my group's names')>
<Group(name='Another one of my group's names')>
print(group.name)
File "\Python\Python37-32\lib\encodings\cp1252.py", line 19, in encode
return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode characters in position 27-28: character maps to <undefined>
因此,这次系统实际上返回了我所在的几个组。但是由于某种unicode错误,因此调用了异常。
不更改任何代码,我也会收到此错误:
Traceback (most recent call last):
File "\Python\Python37-32\lib\site-packages\groupy\session.py", line 49, in data
return self.json()['response']
File "\Python\Python37-32\lib\site-packages\requests\models.py", line 808, in json
return complexjson.loads(self.text, **kwargs)
File "\Python\Python37-32\lib\json\__init__.py", line 348, in loads
return _default_decoder.decode(s)
File "\Python\Python37-32\lib\json\decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "\Python\Python37-32\lib\json\decoder.py", line 355, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 60865 (char 60864)
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "\Eclipse\eclipse-workspace\Gort's Cousin\groupme_bot.py", line 10, in <module>
groups = list(client.groups.list_all())
File "\Python\Python37-32\lib\site-packages\groupy\pagers.py", line 61, in autopage
self.items = self.fetch_next()
File "\Python\Python37-32\lib\site-packages\groupy\pagers.py", line 51, in fetch_next
return self.fetch()
File "\Python\Python37-32\lib\site-packages\groupy\pagers.py", line 42, in fetch
return self.endpoint(**self.params)
File "\Python\Python37-32\lib\site-packages\groupy\api\groups.py", line 21, in _raw_list
return [Group(self, **group) for group in response.data]
File "\Python\Python37-32\lib\site-packages\groupy\session.py", line 51, in data
raise exceptions.InvalidJsonError(self._resp) from e
groupy.exceptions.InvalidJsonError: The JSON was incomplete/invalid
我很可能是错误的,但是在我看来,groupy聚合组的方式和/或我如何设置项目以处理这些调用存在一些问题。
任何有关此问题的帮助都将非常棒。
如果有必要,我还可以从分组库中添加一些代码。
TLDR:使用Groupy在Python中编写groupme机器人时遇到“ UnicodeEncodeError”错误
答案 0 :(得分:0)
已解决: 这是一个问题,带有表情符号的组名与我在eclipse中作为全局设置使用的Cp1252编码配合使用时效果不佳。我更改了文件的运行配置,以编码为UTF-16BE。表情符号无法正确打印到控制台,但是所有组均已正确打印。