Python: ValueError:以10为基数的int()无效的文本:“错误”。

时间:2021-09-18 00:49:55

I have a code that reads the info from a file (lines describe Points, Polygons, Lines and Circles) and parses it into according class. Point has x and 7 coordinates and Line has a starting point and an end point.

我有一个从文件中读取信息的代码(线条描述点、多边形、线条和圆圈),并将其解析为类。点有x和7坐标,直线有起点和终点。

I have a list (line = ['L1','L((1,1), (1,2))','# comment']) and I try to make it into a line. The problem is with creating the end point, when executing I get the following error ValueError: invalid literal for int() with base 10: '' fr the variable x2

我有一个列表(line = 'L1','L(1,1),(1,2))','#注释'],我试着把它变成一行。问题是在创建结束点时,当执行时,我得到了以下错误ValueError: int()与基数10的无效文字:“fr变量x2”。

What is the problem?

这个问题是什么?

Code:

代码:

def make_line(line):
        name = line[0]
        point = line[1].split(", ")
        p = point[0].split(",")
        x1 = int(p[0][3:])
        y1 = int(p[1][:-1])
        point1 = Point(x1,y1)
        p = point[1].split(",")
        x2 = int(p[0][1:])
        y2 = int(p[1][:-2])
        point2 = Point(x2,y2)
        line = Line(point1,point2)
        shapes[name] = line

3 个解决方案

#1


6  

You error message says you are trying to convert an empty string to an int. Double check all your data immediately before doing the conversion to verify this.

您的错误消息说您正在尝试将一个空字符串转换为一个整数。在进行转换之前,请立即检查所有的数据。

It is an absolute certainty that if the data is correct, the cast to integer will work. Therefore, the only conclusion to be drawn is that your data is incorrect. You are claiming in comments to your question that the data is good, but that simply cannot be true.

如果数据是正确的,那么转换成整数将是绝对肯定的。因此,得出的唯一结论是,你的数据是错误的。你在对你的问题的评论中声称数据是好的,但是这是不可能的。

Trust the python interpreter over your own assumptions.

信任python解释器,而不是您自己的假设。

#2


0  

I removed the parts that you did not provide enough info about and I am still not getting the same error.

我删除了你没有提供足够信息的部分,我仍然没有得到相同的错误。

def make_line(line):
    name = line[0]
    point = line[1].split(", ")
    p = point[0].split(",")
    x1 = int(p[0][3:])
    y1 = int(p[1][:-1])
    p = point[1].split(",")
    x2 = int(p[0][1:])
    y2 = int(p[1][:-2])
    return x1, y1, x2, y2

>>> make_line(['L1','L((1,2), (1,1))','# comment'])
(1, 2, 1, 1)

#3


0  

The error says there's something wrong with your code, but it does in fact run correctly. If you ever get errors, it's worth finding the line where they occur, and using print to print their values to make absolutely sure you're passing the right values to the right methods.

错误提示您的代码有问题,但它实际上是正确运行的。如果出现错误,就值得查找它们发生的位置,并使用print来打印它们的值,以确保您将正确的值传递给正确的方法。

An easy way to deal with this (at first glance) funny format is to use python's eval() function. You'll notice that the 2nd part of the list looks a lot like a set of two sets, and in fact it is.

使用python的eval()函数是一种简单的处理方法(乍看之下)。您会注意到,列表的第二部分看起来很像一组两组,实际上是。

If you do this, you'll get a nice set object out:

如果你这样做,你会得到一个漂亮的集合对象:

eval("((1,1), (1,2))")
# equivalent to this:
eval(line[1][1:],{}) # passing an empty dict as the 2nd argument makes eval a (bit) safer

but this is just a quick and dirty method, and should never be used in production code.

但这只是一种快速而又肮脏的方法,不应该在生产代码中使用。

#1


6  

You error message says you are trying to convert an empty string to an int. Double check all your data immediately before doing the conversion to verify this.

您的错误消息说您正在尝试将一个空字符串转换为一个整数。在进行转换之前,请立即检查所有的数据。

It is an absolute certainty that if the data is correct, the cast to integer will work. Therefore, the only conclusion to be drawn is that your data is incorrect. You are claiming in comments to your question that the data is good, but that simply cannot be true.

如果数据是正确的,那么转换成整数将是绝对肯定的。因此,得出的唯一结论是,你的数据是错误的。你在对你的问题的评论中声称数据是好的,但是这是不可能的。

Trust the python interpreter over your own assumptions.

信任python解释器,而不是您自己的假设。

#2


0  

I removed the parts that you did not provide enough info about and I am still not getting the same error.

我删除了你没有提供足够信息的部分,我仍然没有得到相同的错误。

def make_line(line):
    name = line[0]
    point = line[1].split(", ")
    p = point[0].split(",")
    x1 = int(p[0][3:])
    y1 = int(p[1][:-1])
    p = point[1].split(",")
    x2 = int(p[0][1:])
    y2 = int(p[1][:-2])
    return x1, y1, x2, y2

>>> make_line(['L1','L((1,2), (1,1))','# comment'])
(1, 2, 1, 1)

#3


0  

The error says there's something wrong with your code, but it does in fact run correctly. If you ever get errors, it's worth finding the line where they occur, and using print to print their values to make absolutely sure you're passing the right values to the right methods.

错误提示您的代码有问题,但它实际上是正确运行的。如果出现错误,就值得查找它们发生的位置,并使用print来打印它们的值,以确保您将正确的值传递给正确的方法。

An easy way to deal with this (at first glance) funny format is to use python's eval() function. You'll notice that the 2nd part of the list looks a lot like a set of two sets, and in fact it is.

使用python的eval()函数是一种简单的处理方法(乍看之下)。您会注意到,列表的第二部分看起来很像一组两组,实际上是。

If you do this, you'll get a nice set object out:

如果你这样做,你会得到一个漂亮的集合对象:

eval("((1,1), (1,2))")
# equivalent to this:
eval(line[1][1:],{}) # passing an empty dict as the 2nd argument makes eval a (bit) safer

but this is just a quick and dirty method, and should never be used in production code.

但这只是一种快速而又肮脏的方法,不应该在生产代码中使用。