Python - 将字符串转换为浮点数(传统方法 float() 在这种情况下不起作用) [英] Python - convert string to float (the conventional method, float(), is not working in this case)

查看:54
本文介绍了Python - 将字符串转换为浮点数(传统方法 float() 在这种情况下不起作用)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在处理我们的一些数据时,我必须执行非常基本的列条件组合.填充空值后,尝试在分配新变量的列中添加.其中一列最终成为对象,这并非史无前例.然而,我发现看似有效的值不会转换为浮点数(例如 4,789.67).经过多次搜索,似乎我所看到的每个解决方案都指向存在不规则字符(它没有描述我的情况).因此,我尝试在 IPython 中进行实验以重现错误,并且我成功了.但是,我不明白为什么会出现此错误:

In working with some of our data, I had to perform a pretty basic conditional combinations of columns. After filling null values, attempted to add to columns in the assignment of a new variable. One of the columns ended up being object, which is not at all unprecedented. What I found, however, was that seemingly valid values would not convert to float (e.g. 4,789.67). After much searching, it seems that every solution I have seen points to the existence of an irregular character (which does not describe my case). Consequently, I tried to experiment in IPython to recreate the error, and I was successful. I do not understand, however, why I got this error:

测试

z='4,534.07' #initial assignment
print z
print type(z) #checked type
print repr(z) #tried to reveal hidden characters
print repr(z.replace("'","")) #tried to remove excess quotes
print z[1:-1] #tried again to remove excess quotes
print float(z) #failed conversion attempt

输出

4,534.07
<type 'str'>
'4,534.07'
'4,534.07'
,534.0


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-70-8a3c46ebe6ab> in <module>()
      6 print z[1:-1]
      7 print z
----> 8 print float(z)

ValueError: invalid literal for float(): 4,534.07

我所看到的基本转换问题的解决方案总是建议以下将x"转换为 float -->> float(x).如果有人能解释我遗漏了什么,我将不胜感激.(我以前没有发生过这种情况.)

The solutions I have seen for the basic conversion question invariably suggest the following for conversion of 'x' to float -->> float(x). I would be very grateful for anyone who can explain what I have missed. (I have not had this happen before.)

我一直在使用 Enthought 平台:

I have been using the Enthought platform:

发行说明天篷 1.0.0.1160

Release notes Canopy 1.0.0.1160

天篷 1.0.0

首次发布.请参阅文档浏览器、Canopy 用户指南以获取发布信息描述新功能以及任何已知问题和解决方法的注释

First release. See Documention Browser, Canopy Users Guide for release notes describing what's new and any known issues and workarounds

谢谢

推荐答案

唯一的问题是您必须删除逗号.4,534.07 不是有效的浮点文字,但 4534.07 是.

The only problem is that you have to remove the comma. 4,534.07 is not a valid float literal, but 4534.07 is.

(这正是 ValueError: invalidliteral for float(): 4,534.07 告诉你的,只是它缺少你的意思是……?"建议.)

(That's exactly what the ValueError: invalid literal for float(): 4,534.07 is telling you, except that it's missing the "did you mean…?" suggestion.)

所以:

z='4,534.07'
print float(z.replace(',', ''))

<小时>

此外,所有这些删除多余引号"的尝试都不起作用,因为字符串中没有引号.当然,当你打印出字符串的 repr 时会有引号,但这并不意味着它们在字符串本身中,它意味着任何的 repr字符串包含在一对额外的引号中.由于这些引号不在字符串中,因此它们不能影响您对该字符串调用的任何函数(除非该函数做了一些非常非常愚蠢的事情,例如在其参数上调用 repr 以构建一个用于在...上调用 eval 的字符串.


Also, all those attempts to "remove excess quotes" do nothing because there are no quotes in the string. Of course there are quotes when you print out the repr of the string, but that doesn't mean they're in the string itself, it means that the repr of any string is enclosed in an extra pair of quotes. Since those quotes aren't in the string, they can't influence any function you call on that string (unless that function does something really, really stupid, like calling repr on its argument to build up a string to call eval on…).

另外,即使问题过多的引号,只需print z[1:-1]print z.replace("'", "") 实际上不会z 中删除它们,它只会打印出如果你这样做了它会是什么样子.要真正改变 z 的值,你必须给它赋值.例如,如果您将 print z.replace(',', '') 添加到现有代码中,float(z) 仍然会失败.但是如果你加上 z = z.replace(',', ''),那么 float(z) 就会成功.

Plus, even if the problem were excess quotes, just print z[1:-1] or print z.replace("'", "") wouldn't actually remove them from z, it would just print out what it would look like if you had done so. To actually change the value of z, you have to assign something to it. For example, if you add print z.replace(',', '') to your existing code, the float(z) will still fail. But if you add z = z.replace(',', ''), then the float(z) will succeed.

这篇关于Python - 将字符串转换为浮点数(传统方法 float() 在这种情况下不起作用)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆