【发布时间】: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 文件。没有它,我的代码似乎无法工作!所以我不完全确定真相!