NOWSHUT 2023-06-28 18:46 采纳率: 50%
浏览 67
已结题

ldif3读取ldif文件报错:ValueError: subsection not found

使用ldif3模块的LDIFParser读取ldif文件,但一直会有如下报错:

from ldif3 import LDIFParser

dn_attr_dict = {}
parser = LDIFParser(open('adam.ldif', 'rb'))
# parser = LDIFParser(open('correctadam.ldif', 'rb'))
for dn, entry in parser.parse():
        try:
                    dn_attr_dict[dn] = entry
        except:
                    print("error")
print(dn_attr_dict)

报错输出

Traceback (most recent call last):
  File "C:\Users\useful_tools\htd.py", line 20, in <module>
    for dn, entry in parser.parse():
  File "C:\Users\AppData\Local\Programs\Python\Python38\lib\site-packages\ldif.py", line 253, in parse
    yield self._parse_entry_record(block)
  File "C:\Users\AppData\Local\Programs\Python\Python38\lib\site-packages\ldif.py", line 365, in _parse_entry_record
    attr_type, attr_value = self._parse_attr(line)
  File "C:\Users\AppData\Local\Programs\Python\Python38\lib\site-packages\ldif.py", line 318, in _parse_attr
    colon_pos = line.index(b":")
ValueError: subsection not found

读取correctadam.ldif可以正常输出内容,但读取adam.ldif则会报错,对比了这两个文件的大小,前者比后者小很多。请问大家该如何修复这个问题?

  • 写回答

5条回答 默认 最新

  • 技术宅program 2023-06-28 23:57
    关注
    获得1.50元问题酬金

    line中不存在":"时,line.index(b":")会触发ValueError,报错信息为"subsection not found"。
    所以,很可能是adam.ldif文件中的某一行属性格式错误,不存在":"将属性名称和值分隔开来,导致ldif3模块解析出错。

    评论

报告相同问题?

问题事件

  • 系统已结题 7月6日
  • 创建了问题 6月28日