3

I got this error

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

the code of 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])

sample of data like

1   1   0.000000
1   2   26.232388
1   3   44.486252
1   4   47.168839
1   5   37.593277

sample of the other file is

-82.3602 158.46
-91.0108 133.695
-125.815 148.936
-129.259 153.42
user1
  • 501
  • 2
  • 9
  • 24
  • 1
    Never do `.split(' ')` unless you *specifically* want to split on only a single space. I imagine you actually wanted `.split()`, which splits on one or more arbitrary whitespace characters. Also, you could easily debug this yourself by adding `print(content)` on the line before the `assert`. – SethMMorton Feb 06 '21 at 21:39
  • Yeah, there are clearly multiple spaces between those columns... – John Gordon Feb 06 '21 at 21:41
  • i tried it and error still assert(len(content) == 3) AssertionError – user1 Feb 06 '21 at 21:44
  • Did you try `print(content)`? Because if you do it should become apparent why it is failing, You should post that output. – SethMMorton Feb 06 '21 at 21:46
  • Also, your traceback does not match the code - if you can demonstrate that it is actually not related to `split()` I can re-open the question. – SethMMorton Feb 06 '21 at 21:47
  • i got the code from here github.com/lanbing510/DensityPeakCluster/blob/master/cluster.py i checked it again but it didn't work my data as I posted here and as the example of the code – user1 Feb 06 '21 at 21:49
  • Are you sure that _every_ line in the file has three columns? – John Gordon Feb 06 '21 at 21:49
  • yes. because there is another class of code I run it and got the results as the example – user1 Feb 06 '21 at 21:50
  • @user1 The code you posted does not exist exactly in the code that you linked, which makes me believe you have a custom version that you have edited. Therefore, *please* insert `print(content)` before the `assert` statement. Without the output of that it is literally impossible for anyone to help you. – SethMMorton Feb 06 '21 at 21:51
  • i wrote it and got the first values from another file as ['-82.3602', '158.46'] I will edit the post with samples of another file but I thought It got values from distance because I have two file .. one for x and y columns and the other for the distances between them – user1 Feb 06 '21 at 21:55
  • You have your answer right there - that other file has only two columns, so your assertion that the length is 3 fails. – SethMMorton Feb 06 '21 at 21:57
  • the problem is the code is opensource and should work so I wonder why this error appeared and the data is appropriate like the code example – user1 Feb 06 '21 at 21:57
  • it will be 2 ? did you mean that ? – user1 Feb 06 '21 at 21:57
  • but how it will 2 and the next line is idx1, idx2, dis = int(content[0]), int(content[1]), float(content[2]) that will accept 3 ? – user1 Feb 06 '21 at 21:58
  • The length of `['-82.3602', '158.46']` is 2. I cannot help you with why the author wrote the code that way, and if you are or are not using the code correctly, but if you give a file with two columns it will fail because the length is 2, not 3. – SethMMorton Feb 06 '21 at 21:58
  • "but how it will 2 and the next line is idx1, idx2, dis = int(content[0]), int(content[1]), float(content[2]) that will accept 3" This is why the `assert` is there, to avoid an error on that line. This is not the forum to discuss design decisions of an open source project. Please file an issue on that GitHub page. – SethMMorton Feb 06 '21 at 21:59
  • @SethMMorton you are right i checked the files and the wrong was in the file I used so I exchanged it with split() and worked but got another problem here https://stackoverflow.com/questions/66082634/how-can-i-handle-the-code-to-avoid-killed of you can help .. thanks – user1 Feb 06 '21 at 22:31

1 Answers1

2

You get an AssertionError, because the assertion fails, and it fails, because you are splitting on 1 space, but the values are separated by 2 spaces. To circumvent this, use split without arguments, which will split at arbitrary amounts of spaces:

content = line.strip().split()
TheEagle
  • 5,808
  • 3
  • 11
  • 39