Python:ValueError:无效的文字为int()与基10:''错误[关闭](Python: ValueError: invalid literal for int() w

系统教程 行业动态 更新时间:2024-06-14 17:03:52
Python:ValueError:无效的文字为int()与基10:''错误[关闭](Python: ValueError: invalid literal for int() with base 10: '' error [closed])

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

我有一个列表( line = ['L1','L((1,1), (1,2))','# comment'] ),我尝试将它排成一行。 问题是与创建终点,执行时,我得到以下错误ValueError: invalid literal for int() with base 10: '' fr变量x2

问题是什么?

码:

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

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.

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

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

最满意答案

你错误消息说你试图将一个空字符串转换为一个int。 在进行转换之前请仔细检查您的所有数据以验证这一点。

这是一个绝对的确定性,如果数据是正确的,转换为整数将工作。 因此,唯一需要得出的结论是您的数据不正确。 你在评论你的问题时声称数据是好的,但那根本不可能是真的。

根据自己的假设信任python解释器。

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.

更多推荐

本文发布于:2023-04-24 12:17:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/3a1441d7cb81b77ca3620ed1a6c87e5f.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:错误   文字   int   ValueError   Python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!