属性错误列表对象没有属性条

2024-04-26 10:14:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在编写一些代码来标记一个文件,它会查看前一行是否有SCI标记,如果有,则在第五列(在制表符分隔的文件中)用SCI_NXT标记当前行。在

但是,我得到了一个属性错误,即当第一行不是一个项目行是变量的对象时,我试图剥离列表(在line previous_line=split_line(previous_line))。我理解这是因为它把行写成列表,而不是字符串,但我不知道我该如何纠正它。我尝试过使用“extend”,但这导致第一行被写为每个字符都是不同的元素,这也不是我要做的。在

以下是我正在处理的测试文本:

</s>
<s>
Diptera NP  Diptera-n        SCI
was VBD be-v
the DT  the-x
most    RBS most-a
common  JJ  common-j
prey    NN  prey-n
among   IN  among-i
the DT  the-x
insects NNS insect-n
potentially RB  potentially-a
available   JJ  available-j
to  IN  to-i

代码如下:

^{pr2}$

以及trackback错误:

Traceback (most recent call last):
  File "/home/sandra/git/trophic/tagging/tagging_NEXT.py", line 123, in <module>
    main('test_next.vert', 'zenodo_tagged_SCI_MOD.vert')
  File "/home/sandra/git/trophic/tagging/tagging_NEXT.py", line 120, in main
    tag_vert_sci_next('test_next.vert', fname_out)
  File "/home/sandra/git/trophic/tagging/tagging_NEXT.py", line 78, in tag_vert_sci_next
    taggedlines = tag_next_sci(lines)
  File "/home/sandra/git/trophic/tagging/tagging_NEXT.py", line 31, in tag_next_sci
    taggedlines.append(tagline_next_sci(line, taggedlines))
  File "/home/sandra/git/trophic/tagging/tagging_NEXT.py", line 43, in tagline_next_sci
    previous_line = split_line(previous_line)
  File "/home/sandra/git/trophic/tagging/tagging_NEXT.py", line 14, in split_line
    line = line.strip().split()
AttributeError: 'list' object has no attribute 'strip'

Tags: inpygithomelinefilenextsci
2条回答

谢谢你们的帮助。下面是我最后得到的代码:

    """Tags a file with SCI_MOD in extra feature column. Reads and writes vert files.
"""
import json


VFILE = 'zenodotaggedWS_ALL.vert'

def split_line(line):
    """Split a line into its parts"""
    line = line.strip().split()
    if len(line) == 1:
        word = line[0]
        pos, lempos, tag ="", "", ""
    elif len(line) == 3:
        word, pos, lempos = line
        tag = ""
    elif len(line) == 4:
        word, pos, lempos, tag = line
    return [word, pos, lempos, tag]

def tag_next_sci(lines):
    """Loops through lines of original document to add to new file (tagged)
    """
    taggedlines = []
    for line in lines:
        taggedlines.append(tagline_next_sci(line, taggedlines))
    return taggedlines


def tagline_next_sci(line, taggedlines):
    """Assigns an indicator tag to a line
    """
    #<> are structural and do not need to be considered for feature tags so can be committed directly
    if line.startswith('<'):
        return line
    #look back at previous line to see if SCI, if so tag current line
    previous_line  = taggedlines[-1]
    previous_line = split_line(previous_line)
    line = split_line(line)
    if previous_line[2] == "SCI-n":
            print("\t".join(line) + "\tSCI_MOD\n")
            return "\t".join(line) + "\tSCI_MOD\n"
    return line + "\n" if isinstance(line, str) else "\t".join(line) + "\n"

def read_vfile(fname):
    """Reads a vert file
    """
    with open(fname, 'r') as vfile:
        lines = vfile.readlines()
        return lines

def write_vfile(fname, taggedlines):
    """Writes a vert file
    """
    # write to file
    with open(fname, 'w') as outfile:
        outfile.writelines(taggedlines)

def tag_vert_sci_next(fname, fname_out):
    """Creates a new file with tags
    """
    # vertical file location
    # make list of species names
    # read vertical file
    lines = read_vfile(fname)
    # tag file    
    taggedlines = tag_next_sci(lines)
    # call write file
    write_vfile(fname_out, taggedlines)

def main(fname, fname_out):
    #call sci_next tagging
    tag_vert_sci_next('zenodotaggedWS_ALL.vert', fname_out)

if __name__ == "__main__":
    main('zenodotaggedWS_ALL.vert', 'zenodo_tagged_SCIMOD2.vert')

您的问题似乎是tagline_next_sci有时返回一个列表而不是字符串。例如,我尝试在函数中添加一个print来查看发生了什么

...
def tagline_next_sci(line, taggedlines):
    print('taggedlines', taggedlines)
    """Assigns an indicator tag to a line
    """
...

得到了输出

^{pr2}$

因此,您应该检查函数的底部,以确保始终返回一个字符串,如果您需要将列表拼凑成一个字符串,可以使用如下方法执行"\t".join(line)

return line if isinstance(line, str) else "\t".join(line)

相关问题 更多 >