Python 2.7.3
SillyWalk.getGroups(user_id = user)
当我为一个像鹦鹉一样不复存在的用户运行时,我得到了以下错误。
Traceback (most recent call last):
File "C:\Python\API\getgroups.py", line 32, in <module>
Grp = SillyWalk.getGroups(user_id = user)
File "c:\python27\lib\site-packages\SillyWalkapi-1.4.2-py2.7.egg\SillyWalkapi\__init__.py", line 349, in handler
parse_format=args['format'], **args)
File "c:\python27\lib\site-packages\SillyWalkapi-1.4.2-py2.7.egg\SillyWalkapi\__init__.py", line 435, in __wrap_in_parser
return parser(self, data)
File "c:\python27\lib\site-packages\SillyWalkapi-1.4.2-py2.7.egg\SillyWalkapi\__init__.py", line 278, in parse_etree
raise SillyWalkError(u'Error: %(code)s: %(msg)s' % err.attrib)
SillyWalkError: Error: 1: User not found
我试图通过在以下try ... except
语句中添加语句来解决问题:
try:
Grp = SillyWalk.getGroups(user_id = user)
g = 0
for group in Grp.find('groups').findall('group'):
g += 1
u_ID = user
g_ID = group.get('_ID')
gName = "" # group.get('name')
HasParrot = group.get('HasParrot')
Priv = group.get('Priv')
Mbr = group.get('members')
threads = group.get('threads')
tup = '{0}\t{1}\t{2}\t{3}\t{4}\t{5}\t{6}\n'.format(u_ID, g_ID, gName.encode('utf-8'), HasParrot, Priv, Mbr, threads)
OutFile.write(tup.encode('utf-8'))
except SillyWalkError:
tup = '{0}\tNo Records Found\n'.format(u_ID)
OutFile.write(tup.encode('utf-8'))
但它给了我一个未定义的错误:
Traceback (most recent call last):
File "C:\Python\API\getgroups.py", line 47, in <module>
except SillyWalkError:
NameError: name 'SillyWalkError' is not defined
>>>
这是我第一次尝试编写异常类,所以有人可以告诉我怎么做吗?由于SillyWalk.getGroups
各种错误原因,有十几个不同的错误编号。每个人都需要一个箱子吗?
我将永远欠你的债!(你永远不会从我这里收集任何东西,但我仍然欠你的!)
顺便说一句:在深入了解我的脚本时,有人可以告诉我如何解决我的gName
问题,所以我不需要将其归零吗?它不断给我:
Traceback (most recent call last):
File "C:\Python\API\getgroups.py", line 44, in <module>
OutFile.write(tup.encode('utf-8'))
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 54: ordinal not in range(128)