for循环中的Python ldif3解析器和异常

时间:2016-10-06 07:16:53

标签: python

来自网站:https://pypi.python.org/pypi/ldif3/3.2.0

我有这段代码:

from ldif3 import LDIFParser
from pprint import pprint

parser = LDIFParser(open('data.ldif', 'rb'))
for dn, entry in parser.parse():
    print('got entry record: %s' % dn)
    pprint(record)

现在,阅读我的文件data.ldif我在parser.parse()中有例外。 问题是如何捕获此异常并允许循环转到下一条记录(继续)?

引用:

Traceback (most recent call last):
  File "ldif.py", line 16, in <module>
    for dn, entry in parser.parse():
  File "/home/dlubom/anaconda2/lib/python2.7/site-packages/ldif3.py", line 373, in parse
    yield self._parse_entry_record(block)
  File "/home/dlubom/anaconda2/lib/python2.7/site-packages/ldif3.py", line 346, in _parse_entry_record
    attr_type, attr_value = self._parse_attr(line)
  File "/home/dlubom/anaconda2/lib/python2.7/site-packages/ldif3.py", line 309, in _parse_attr
    return attr_type, attr_value.decode('utf8')
  File "/home/dlubom/anaconda2/lib/python2.7/encodings/utf_8.py", line 16, in decode
    return codecs.utf_8_decode(input, errors, True)
UnicodeDecodeError: 'utf8' codec can't decode byte 0xb3 in position 6: invalid start byte

0 个答案:

没有答案