python 试图删除单个撇号
我正在使用一个名为 CityEngine 的程序,它有一个 python 元素。
问题:我刚刚在一个对象上调用了一个函数,并返回了一个数字列表 xyz。我把xyz分成了他们自己的名字。我还调用一个函数来检索与该对象相关的不同属性,以替换之前检索到的 y 值。
现在,当我打印 y 值时,它仅包含除小数位之外的数字字符。 当我将 y 值合并到新列表中时,它的值周围有一个撇号。
例如, print(y) 返回 5.0000000
如果我将其放置为这样的位置[x,y,z],我会得到 [0, '5.000000' , 0] 的 print(position)。该程序无法读取单个撇号,因此完全忽略该值。
我尝试过 .remove("'","") 和 .strip() 但什么也没有。
任何帮助将不胜感激。
谢谢。
I'm using a program called CityEngine which has a python element to it.
The problem: I've just called a function on an object and returns me a list of numbers, xyz. I split the xyz into their own names. I also call a function to retrieve a different attribute related to this object to replace the previously retrieved y value.
Now, when I print the y value, it contains numerical characters only apart from decimal place.
When I incorporate the y value into a new list, it's value has single apostrophe around it.
For example, print(y) returns 5.0000000
If I place it like this position[x,y,z] I get a print(position) of [0, '5.000000' , 0]. The program can't read the single apostrophes so ignored the value completely.
I've tried .remove("'","") and .strip() and nothing.
Any help would be appreciated.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看起来更像是该函数返回的不是数字而是字符串。因此,为了处理它,您必须使用
int()
或float()
转换字符串。一般来说,如果您对某些项目列表执行
print(l)
,则每个项目都将打印其__repr__
方法的输出。约定,字符串的 __repr__ 方法用单个撇号包裹字符串,而数字则不会包裹。这是为了消除潜在的歧义。因此,返回的print(l)
将是一个包含
int
、str
和float
的列表。That looks more as if the function were not returning a number but a string. So, in order to deal with it, you’ll have to convert the string using either
int()
orfloat()
.In general, if you do a
print(l)
on some list of items, each item will be printed with the output of it’s__repr__
method. Convention has it that the__repr__
method of string wraps the string with single apostrophes, whereas numbers do not get wrapped. This is to remove potential ambiguity. Hence, aprint(l)
which returnedwould be a list containing an
int
, astr
and afloat
.将其转换为浮点数...它是一个字符串,因此您需要进行字符串到浮点数的转换
Convert it to float ... It is a string, so you need to do string to float conversion