在 Python 中,我的 .split() 行中有一个值错误,returns 1 expected 2。添加白色 space 没有帮助。 data.txt 有问题?

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?

如果该标题含糊不清,我们深表歉意。首先,我想说的是,我已经通读了所有我能找到的关于 python 中的值错误的论坛,我已经实现了它们,但我的程序仍然无法通过第 22 行。在底部但首先我会给你 "DL" 哈哈。

我正在编写一个读取 data.txt 文件并显示输出的程序。这是项目的目标

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.

我的命令行错误是:

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)

我写的代码是:

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()

并且 data.txt 文件包含:

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

在第 22 行我尝试添加到 .split(),我添加了 (" "), (", ") none 其中成功了。我试图将变量从 int 更改为 float,但我无法连接字符串。所以我很茫然,有人介意帮我找出问题所在吗?非常感谢!

我修复了您代码中的缩进,凭直觉知道 while 循环在文件处理中结束的位置。我还更改了 file 变量名称,因为 file 是您所做的预定义类型 而不是 真的想重新定义。

该程序似乎有效。

in_file = open("data.txt")

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

while line != 'Q':

    numVertices = in_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 = in_file.read(8).split( )
        print("Vertex " + str(i + 1) + " has coordinates at (" + x + ", " + y + ")")

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

in_file.close()

输出:

Reading vertices for polygon with 5 vertices...
Vertex 1 has coordinates at (1.7, 4.9)
Vertex 2 has coordinates at (6.1, 6.2)
Vertex 3 has coordinates at (7.0, 2.8)
Vertex 4 has coordinates at (4.8, 0.1)
Vertex 5 has coordinates at (1.5, 1.4)
Reading vertices for rectangle having 4 vertices...
Vertex 1 has coordinates at (7.0, 5.0)
Vertex 2 has coordinates at (1.0, 5.0)
Vertex 3 has coordinates at (1.0, 3.0)
Vertex 4 has coordinates at (7.0, 3.0)
Reading vertices for rectangle having 4 vertices...
Vertex 1 has coordinates at (4.1, 5.4)
Vertex 2 has coordinates at (6.9, 2.5)
Vertex 3 has coordinates at (2.9, 0.8)
Vertex 4 has coordinates at (0.9, 2.5)
Reading vertices for polygon with 3 vertices...
Vertex 1 has coordinates at (1.2, 4.7)
Vertex 2 has coordinates at (6.5, 4.2)
Vertex 3 has coordinates at (4.0, 1.7)

您的代码存在问题,它需要非常 指定的格式。观察你的所作所为:

read(1)

正好读取一个字节。大概期待 polygon/quit 的类型。那么:

read(3).strip()

为什么是 3 个字节?好吧,您可能希望字母后有一个 space,然后是指定顶点数量的数字, 必须 为 1 位,最后是换行符。总共 3 个字节。最后,对于每个顶点,你

read(8)

8 个字节 - 每个坐标(数字、小数点、数字)3 个字节,它们之间有一个 space,最后是新行。

如果有任何字符不合适,它会破坏你的代码。您期待准确的格式。如果您不必使用 read,那么就不要使用 - 有更简单的方法,特别是逐行阅读。如果必须使用读取,请确保文件格式正确。您提供的示例中至少有 2 spaces,并且 有时有 3 spaces - 这会破坏您的代码。最后,始终尝试使用打印调试小程序 - 放入 for 循环:

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

会准确地打印出您正在中断的内容,并为您提供有关如何继续的提示。

Welp 好像问题解决了。

我正在研究它并想出了替代解决方案。

喜欢的话点个赞吧,我来分解我的方法

file = open("data.txt")

line = file.readlines()
for i in range(len(line)):
    if 'P' in line[i] or 'R' in line[i]:
        x = line[i].split()
        n_verts = x[1]
        n_verts = int(n_verts)
        print("-"*47)
        print(f"Reading vertices for polygon with {n_verts} vertices...\n")
        for a in range(1, n_verts + 1): 
            verts = line[i+a].split()
            verts = ', '.join(verts)
            print(f"Vertex {a} has coordinates ({verts})")
-----------------------------------------------
Reading vertices for polygon with 5 vertices...

Vertex 1 has coordinates (1.7, 4.9)
Vertex 2 has coordinates (6.1, 6.2)
Vertex 3 has coordinates (7.0, 2.8)
Vertex 4 has coordinates (4.8, 0.1)
Vertex 5 has coordinates (1.5, 1.4)
-----------------------------------------------
Reading vertices for polygon with 4 vertices...

Vertex 1 has coordinates (7.0, 5.0)
Vertex 2 has coordinates (1.0, 5.0)
Vertex 3 has coordinates (1.0, 3.0)
Vertex 4 has coordinates (7.0, 3.0)
-----------------------------------------------
Reading vertices for polygon with 4 vertices...

Vertex 1 has coordinates (4.1, 5.4)
Vertex 2 has coordinates (6.9, 2.5)
Vertex 3 has coordinates (2.9, 0.8)
Vertex 4 has coordinates (0.9, 2.5)
-----------------------------------------------
Reading vertices for polygon with 3 vertices...

Vertex 1 has coordinates (1.2, 4.7)
Vertex 2 has coordinates (6.5, 4.2)
Vertex 3 has coordinates (4.0, 1.7)