0

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)
4

2 回答 2

1

NameError: name 'SillyWalkError' is not defined只是表示名称SillyWalkError未定义。它不是特定于异常处理或编写异常类的。

看起来您只是没有将名称SillyWalkError导入到试图捕获异常的模块中;没有 的错误except表明异常类本身工作正常。

于 2013-02-05T07:30:20.877 回答
0

好的,体育迷们!

至少我对此提出的第一个问题是我众多金发时刻之一的结果!金发时刻!

我正在使用except SillyWalk.SillyWalkError ...而不是except SillyWalk***api***.SillyWalkError ...

感谢所有帮助过的人!

于 2013-02-06T06:25:40.340 回答