python - 如何将datetime对象转换为float? AttributeError : 'datetime.datetime' object has no attribute 'datetime_to_float'

标签 python datetime error-handling

我有以下形式的日期时间数据:
yyyy-mm-dd hh:minmin:secsec + hh:minmin
例如:2020-02-01 01:00:00 + 01:00
我现在想将其转换为浮点数。我使用了找到该问题的解决方案的函数:python datetime to float with millisecond precision

def datetime_to_float(d):
    return d.timestamp()
我使用以下代码将此函数应用于了datetime对象:
time_in_float = []

for i in time:
    time_in_float.append(i.datetime_to_float())
并得到此错误:
    ---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-11-f6272033e480> in <module>()
      2 
      3 for i in time:
----> 4     time_in_float.append(i.datetime_to_float())
      5 

AttributeError: 'datetime.datetime' object has no attribute 'datetime_to_float'
我要在这里更改什么?
非常感谢!

最佳答案

您需要在循环中执行time_in_float.append(datetime_to_float(i))

关于python - 如何将datetime对象转换为float? AttributeError : 'datetime.datetime' object has no attribute 'datetime_to_float' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64871345/

相关文章:

python - 如何在 Google OR-Tools 中设置每条路线的最小位置?

python - 没有名为组合的模块(来自 import wx.combo) - pyspread

python - 仅在一天结束之前查找数据框列的平均值(日期时间索引)

validation - TYPO3 9,将表单验证错误分配给相应的字段

php - 如何在 PHP 中获取有用的错误消息?

python - 在python中使用多个参数进行日志记录

python - eth-brownie - 没有名为 <Users.someuser> 的模块

mysql - 如何最好地在 MySql 中存储日期/时间?

c# - 我如何比较和搜索字符串和日期时间 C#

flutter - RenderCustomMultiChildLayoutBox 的每个 child 都必须在其父数据中有一个 ID