You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

运行linkrot脚本遇TypeError: NoneType与str无法比较的问题求助

解决TypeError: '<' not supported between instances of 'NoneType' and 'str'错误

问题详情

运行linkrot工具时触发如下错误:

TypeError: '<' not supported between instances of 'NoneType' and 'str'

完整回溯信息:

Traceback (most recent call last):
File "c:\python38\lib\runpy.py", line 193, in _run_module_as_main
return _run_code(code, main_globals, None,
File "c:\python38\lib\runpy.py", line 86, in run_code
exec(code, run_globals)
File "C:\Python38\Scripts\linkrot.exe_main.py", line 7, in
File "c:\python38\lib\site-packages\linkrot\cli.py", line 215, in main
text = get_text_output(pdf, args)
File "c:\python38\lib\site-packages\linkrot\cli.py", line 126, in get_text_output
for k, v in sorted(pdf.get_metadata().items()):
TypeError: '<' not supported between instances of 'NoneType' and 'str'.

出错的代码片段:

def get_text_output(pdf, args):
    """ Normal output of infos of linkrot instance """
    # Metadata
    ret = ""
    ret += "Document infos:\n"
    for k, v in sorted(pdf.get_metadata().items()):
        if v:
            ret += "- %s = %s\n" % (k, parse_str(v).strip("/"))

    # References
    ref_cnt = pdf.get_references_count()
    ret += "\nReferences: %s\n" % ref_cnt
    refs = pdf.get_references_as_dict()
    for k in refs:
        ret += "- %s: %s\n" % (k.upper(), len(refs[k]))

    if args.verbose == 0:
        if "pdf" in refs:
            ret += "\nPDF References:\n"
            for ref in refs["pdf"]:
                ret += "- %s\n" % ref
        elif ref_cnt:
            ret += "\nTip: You can use the '-v' flag to see all references\n"
    else:
        if ref_cnt:
            for reftype in refs:
                ret += "\n%s References:\n" % reftype.upper()
                for ref in refs[reftype]:
                    ret += "- %s\n" % ref

    return ret.strip()

错误原因

pdf.get_metadata()返回的字典中存在键为None的条目,调用sorted()对字典项排序时,Python会尝试比较键的大小,但None和字符串类型无法用<运算符比较,因此抛出错误。

修复方案

方案1:过滤掉键为None的无效条目

修改代码中遍历元数据的部分,先过滤掉键为None的项再排序:

def get_text_output(pdf, args):
    """ Normal output of infos of linkrot instance """
    # Metadata
    ret = ""
    ret += "Document infos:\n"
    # 先过滤键为None的条目,再排序
    metadata = pdf.get_metadata()
    filtered_items = [(k, v) for k, v in metadata.items() if k is not None]
    for k, v in sorted(filtered_items):
        if v:
            ret += "- %s = %s\n" % (k, parse_str(v).strip("/"))
    # 后续代码保持不变...

方案2:给None类型的键设置默认字符串

如果不想丢弃这些条目,可以给None类型的键指定一个默认字符串(比如"未知字段"),确保排序时类型统一:

def get_text_output(pdf, args):
    """ Normal output of infos of linkrot instance """
    # Metadata
    ret = ""
    ret += "Document infos:\n"
    # 排序时将None键替换为默认字符串
    for k, v in sorted(pdf.get_metadata().items(), key=lambda x: x[0] if x[0] is not None else "未知字段"):
        if v:
            ret += "- %s = %s\n" % (k if k is not None else "未知字段", parse_str(v).strip("/"))
    # 后续代码保持不变...

两种方案都能解决排序时的类型不兼容问题,方案1更简洁,方案2保留了所有元数据条目。

内容的提问来源于stack exchange,提问作者Marshal Miller

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.24 16:48:35