Search code examples
pythoncommand-linecompiler-errorsreadfilevalueerror

In Python, I have a Value Error in my line with .split(), returns 1 expected 2. Adding white space doesn't help. Issue with data.txt?


Apologies if that title was vague. First I'd like to say that I have read through every forum I can find on the Value Error in python, I've implemented them but my program still can not get past line 22. There is more explanation at the bottom but first I'll give you the "DL" haha.

I am writing a program that reads a data.txt file and displays the output. Here is the goal of the project

Your output should look like: Reading vertices for a polygon with 5 vertices... Vertex 1 has coordinates at (1.70, 4.90) Vertex 2 has coordinates at (6.10, 6.20) Vertex 5 has coordinates at (1.50, 1.40)


Reading vertices for a rectangle having, (of course), 4 vertices... Vertex 1 has coordinates at (7.00, 5.00) ... Your program only needs to echo check the data; there is no requirement during this lab to construct polygon objects, rectangle objects, to do transformations, to undo transformations, or to find and print perimeter.

The error from my command line is:

Reading vertices for polygon with 5 vertices...
Vertex 1 has coordinates at (1.7, 4)
Vertex 2 has coordinates at (.9, 6.1)
Vertex 3 has coordinates at (6.2, 7.0)
Vertex 4 has coordinates at (2.8, 4.8)
Vertex 5 has coordinates at (0.1, 1.)
Reading vertices for polygon with 1 vertices...
Traceback (most recent call last):
File "Read.py", line 22, in <module>
  x, y = file.read(8).split( )
  ValueError: too many values to unpack (expected 2)

The code I have written is:

file = open("data.txt")

line = file.read(1).strip()

while line != 'Q':

    numVertices = file.read(3).strip()
    numVertices = int(numVertices)

if numVertices != 4:
    print("Reading vertices for polygon with " + 
str(numVertices) + " vertices...") 

else:
    print("Reading vertices for rectangle having " + 
str(numVertices) + " vertices...")

for i in range(int(numVertices)):

    x, y = file.read(8).split( )
    print("Vertex " + str(i + 1) + " has coordinates at (" + x + ", " + y + ")")

line = file.read(1).strip()

file.close()

And the data.txt file contains:

P  5
1.7   4.9
6.1  6.2
7.0  2.8
4.8  0.1
1.5  1.4
R  4
7.0  5.0
1.0  5.0
1.0  3.0
7.0  3.0
P  4
4.1  5.4
6.9  2.5
2.9  0.8
0.9   2.5
P  3
1.2  4.7
6.5  4.2
4.0  1.7
Q

In line 22 I have tried adding to the .split(), I have added (" "), (", ") none of which have succeeded. I have tried to change the variables from int to float, but then I can't concatenate a string. So I am at a loss, would someone mind helping me find out what the issue is here? Thank you so much!


Solution

  • The problem with your code it is expecting a very specified format. Observe what you do:

    read(1)
    

    That reads exactly one byte. Presumably expecting the type of polygon/quit. Then:

    read(3).strip()
    

    Why 3 bytes? Well, you are probably expecting a space after the letter, then the number specifying the amount of vertices, which must be 1 digit, and finally - the newline character. 3 bytes total. finally, for every vertex, you

    read(8)
    

    8 bytes - 3 for each coordinate (digit, decimal point, digit), a space between them, and finally the new line.

    If there is any character out of place, it will break your code. You are expecting exact formatting. If you do not have to use read, then do not - there are easier ways, specifically reading line by line. If you must use read, then make sure the formatting of the file is correct. You have at least 2 spaces, and sometimes 3 spaces in your provided sample - that will break your code. Finally, always try to debug small programs with printing - putting in your for loop:

    for i in range(int(numVertices)):
        res = file.read(8).split()
        print(res)
        x,y = res
    

    would have printed exactly what you were breaking on, and gave you a hint on how to proceed.