【问题标题】:Python - why is this a malformed node or string?Python - 为什么这是一个格式错误的节点或字符串?
【发布时间】:2018-03-09 21:48:00
【问题描述】:

我只是在整理一些代码,遇到了一些麻烦。我遇到了一个以前从未遇到过的错误

ValueError: malformed node or string:['0.000', '37.903', 'nosing']

顶部的 openLabels 函数运行良好,并返回您在错误消息中看到的结构化项目列表。我正在做一些调试,发现是 labelsToFrames 函数抛出了错误。它不接受我的列表列表作为输入。我不知道为什么。

任何方向将不胜感激!

def openLabels(pathLabels):
    path = (pathLabels + "*.lab")
    files = glob.glob(path)

    textCorpus = []
    for name in files:
        try:
            with open(name) as f:
                for line in f:
                    line = line.split()
                    textCorpus.append(line)

        except IOError as exc: 
            if exc.errno != errno.EISDIR:
                raise

    return textCorpus

def labelToFrames(labelCorpus):
    with labelCorpus as f_in:
        for line in f_in:
            song = ast.literal_eval(line)

        output = []                                 
        for block in song:
            block_start = float(block[0])           
            block_end = float(block[1])            
            singing = block[2]                      
            block_range = np.arange(block_start, block_end, 0.033)
        for x in block_range:
            ms_start = '{0:.3f}'.format(x)
            ms_end = '{0:.3f}'.format(x + 0.032)
            add_to_output = [ms_start, ms_end, singing]
            output.append(add_to_output)

        return(output)   


def main(): 
    pathLabels = "~/Train Labels/"
    labelCorpus = openLabels(pathLabels)
    labelCorpusFrames = labelToFrames(labelCorpus)

main()  


  File "<ipython-input-7-d1a356f3bed8>", line 1, in <module>
    labelCorpusFrames = labelToFrames(labelCorpus)

  File "<ipython-input-2-77bea44f1f3d>", line 54, in labelToFrames
    song = ast.literal_eval(line)

  File "*/lib/python3.6/ast.py", line 85, in literal_eval
    return _convert(node_or_string)

  File "*/lib/python3.6/ast.py", line 84, in _convert
    raise ValueError('malformed node or string: ' + repr(node))

ValueError: malformed node or string: ['0.000', '37.903', 'nosing']

【问题讨论】:

  • 如果您需要有关错误的帮助,请显示整个回溯。
  • 附加到原件。
  • 你想在这里做什么:song = ast.literal_eval(line)?
  • 当您将列表传递给ast.literal_eval 时,您期望会发生什么?
  • 我在调试。将我的代码从函数中提取出来。打开 1 个 .lab 文件。没有它,我的代码似乎无法工作!所以我不完全确定真相!

标签: python malformed


【解决方案1】:

问题在于labelCorpus 是一个列表列表。因此,当您执行labelToFrames 并传入labelCorpus 时,它会遍历列表列表,将每个单独的列表分配给line,然后尝试在line 上运行ast.literal_eval。然后这会失败,因为ast.literal_eval requires either a string or expression node,而不是列表。

labelCorpus 是一个列表列表的原因在于它被分配了一个来自openLabels 函数的值。在以下部分中,您将遍历 glob.glob 返回的文件路径并打开文件:

with open(name) as f:
    for line in f:
        line = line.split()
        textCorpus.append(line)

在每个打开的文件上,您将遍历各个行,然后拆分文件中的每一行(返回一个列表)并将其分配回line 变量,然后将其附加到textCorpus 列表中.然后,这个列表列表就是您从函数返回并分配给 main 中的 labelCorpus 的内容。

【讨论】:

  • 我想知道这种情况吗?我正在使用 pathLabel 函数加载 3 个实验室文件。但是,所有文件都包含在 1 个列表中。 Lab1 索引 0 到 19,lab2 索引 20 到 51 等等...
  • @CiaranDeCeol 我已经添加了一些关于创建列表的位置的说明。
  • 我明白你在说什么,但我只是想弄清楚如何修复它的逻辑!我想我必须在早上审查这个并停放一段时间。干杯。
【解决方案2】:

我不在家,所以目前无法进行实验,但我认为问题在于实数与字符串混合。尝试先将它们转换为字符串,然后将它们传递给正在使用它们的任何函数。几个月前我遇到过一次,提前转换就是答案。

【讨论】:

    猜你喜欢
    • 2020-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多