【问题标题】:PYTHON: Simple Float conversion not working why?PYTHON:简单的浮点转换不起作用,为什么?
【发布时间】:2018-05-23 07:29:52
【问题描述】:

为什么这个简单的转换不起作用请帮助:

print(getx)

Date
2017-12-08   -4.484884
Name: CCI, dtype: float64

chkccisplt=getx
type(getx)
chksplt=str(getx)
type(chksplt)
print(chksplt)
Date
2017-12-08   -4.484884
Name: CCI, dtype: float64
chkdt,chkcci=chksplt.split("  ")
print(chkdt)
Date
2017-12-08
print(chkcci)
-4.484884
Name: CCI, dtype: float64
chkcci=chkcci.strip()
chkcciflt=float(chkcci)

**ValueError: could not convert string to float: '-4.484884\nName: CCI, dtype: 
float64'**

在上面的最后一行中,返回值错误。 提前一千感谢您的所有回答。

【问题讨论】:

  • 再次阅读错误信息。字符串'-4.484884\nName: CCI, dtype: float64' 不是正确的浮点数。

标签: python valueerror


【解决方案1】:

改变这一行

chkcci=chkcci.strip()

chkcci=chkcci.split('\n')[0].strip()

您需要删除“名称:”部分才能将数据转换为浮点数。

【讨论】:

  • Yagccept 回答 saar。
猜你喜欢
  • 1970-01-01
  • 2010-12-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多