0

我正在编写以下函数来格式化文件对象“项目”中的一些文本:

def clean(item):
    lines = open(str(item)).readlines()
    outp = []
    switch = 1

    for line in lines:
        line = line.strip()
        if line:
            line = re.sub(' +', ' ', line)
            if '%' in line:
                line = line.replace('%', ' per cent')
            if line.startswith('Note'):
                switch = 2

            if line.startswith('l\t'):
                line = line.split('\t')
                line[0] = '<@01_bullet_point>l<@$p>'
                line = '\t'.join(line)
                outp.append(get_prefix(switch,1) + line)
            else:
                outp.append(get_prefix(switch,2) + line)
            outp.append('\n')
    return ''.join(outp)

我得到一个 TypeError: unsupported operand types for +: 'NoneType' and 'str'

我四处搜索解决方案,但找到了http://www.skymind.com/~ocrow/python_string/,其解决方案 4 似乎证实我在正确的轨道上。

这是引用的其他函数已确认工作,所以我知道问题必须在这里,但是,试图找到相关的 SO 问题并检查我的 Python 副本简而言之,我不知道为什么我得到错误。我最好的猜测是与变量范围有关,但我看不到它,而且,再次搜索,我无法理解为同样的问题。我希望我遗漏了一些明显的东西,但任何帮助将不胜感激。

编辑 get_prefix 是:

def get_prefix(section, type):
    if section == 1:
        if type == 1:
            prefix = '@01_bullets:'
        elif type == 2:
            prefix = '@04_section_sub_subhead:'
        else:
            prefix = '@06_body_text:'

    else:
        if type == 2:
            prefix = '@14_notes_sub_sub_heading:'
        else:
            prefix = '@16_notes_text:'

    return prefix

我看不出它如何返回无。

完整的追溯是:

File "rep_builder.py", line 65, in <module>
`rep.write(clean(item))`
File "rep_builder.py", line 36, in clean
`outp.append(get_prefix(switch,2) + line)`
TypeError: unsupported operand types for +: 'NoneType' and 'str'

抱歉更新缓慢,我在两台无法连接的机器上工作(长篇大论)。

4

3 回答 3

4

The only + operation is outp.append(get_prefix(switch,1) + line) and similar outp.append(get_prefix(switch,2) + line) - that means that get_prefix() is the culprit, returning None here.

于 2012-05-15T17:38:55.293 回答
0

I believe your call to get_prefix is resulting in None being returned.

Given the error message and the available source code, this would be the most likely cause as it's the only place you are trying to concatenate something unknown and a string using the + operator. I.e.,

outp.append(get_prefix(switch,1) + line)

or

outp.append(get_prefix(switch,2) + line)

EDIT: one approach to check your get_prefix function is to give your prefix variable a default string value at the start of the function, and see if that eliminates the problem. That would point to that function, though I have to agree, I don't see how that code could return None as shown now.

于 2012-05-15T17:39:38.333 回答
0

检查你的缩进。你确定get_prefix看起来不像这样吗?(我问是因为缩进clean是错误的):

def get_prefix(section, type):
    if section == 1:
        if type == 1:
            prefix = '@01_bullets:'
        elif type == 2:
            prefix = '@04_section_sub_subhead:'
        else:
            prefix = '@06_body_text:'

    else:
        if type == 2:
            prefix = '@14_notes_sub_sub_heading:'
        else:
            prefix = '@16_notes_text:'

        return prefix

如上缩进,如果等于get_prefix则返回。Nonesection1

于 2012-05-15T18:29:58.420 回答