Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 断言(len(content)==3)断言错误_Python_Assert - Fatal编程技术网

Python 断言(len(content)==3)断言错误

Python 断言(len(content)==3)断言错误,python,assert,Python,Assert,我犯了这个错误 cluster.py", line 20, in load_data distance, num, max_dis, min_dis = load_data(distance_file) assert(len(content) == 3) AssertionError cluster.py的代码 with open(distance_file, 'r', encoding = 'utf-8') as infile: for line in

我犯了这个错误

cluster.py", line 20, in load_data
   distance, num, max_dis, min_dis = load_data(distance_file)
    assert(len(content) == 3)
AssertionError
cluster.py的代码

with open(distance_file, 'r', encoding = 'utf-8') as infile:
        for line in infile:
            content = line.strip().split(' ')
            assert(len(content) == 3)
            idx1, idx2, dis = int(content[0]), int(content[1]), float(content[2])
数据样本,如

1   1   0.000000
1   2   26.232388
1   3   44.486252
1   4   47.168839
1   5   37.593277
另一个文件的示例是

-82.3602 158.46
-91.0108 133.695
-125.815 148.936
-129.259 153.42

您会得到一个
AssertionError
,因为断言失败,并且它失败了,因为您在1个空格上拆分,但是值被2个空格分隔。要避免这种情况,请使用不带参数的拆分,这将以任意数量的空格进行拆分:

content=line.strip().split()

除非您特别希望仅在单个空间上拆分,否则切勿执行
.split(“”)
。我想您实际上想要
.split()
,它可以在一个或多个任意空白字符上拆分。另外,您可以通过在
断言
之前的行中添加
print(content)
来轻松调试。是的,这些列之间显然有多个空格……我尝试了,但仍然错误断言(len(content)==3)断言错误您尝试了
print(content)
?因为如果你这样做,它失败的原因应该很明显,你应该发布该输出。此外,你的回溯与代码不匹配-如果你能证明它实际上与
split()
无关,我可以重新打开这个问题。我试过了,错误仍然存在(len(content)==3)AssertionError@user1很抱歉但我试过了,效果很好