将字符串时间戳转换为Python日期时间(是:如何在Python中正确地将char转换为float或double)
这里我有这样的字符串“1322485986.672901000”
,数据类型是代表unixtime的字符串。我想在 Python 中将其转换为日期时间。
我使用的方式是 date = datetime.fromtimestamp(float(row[0]))
row[0] 表示像这样的值 1322485986.672901000
,因为我有几行。转换结果是错误的,它只计算点之前的数字。所以转换后的日期就像 2011-11-28 13:53:23.6729
我认为问题是 float(row[0])
,但我不知道如何解决这个问题,或者有谁知道如何以更好的方式将unixtime转换为datetime?
非常感谢!
现在我得到的结果是 2011-11-28 14:13:06.672901
但是当使用在线转换器时,结果是 2011-11-28 13:13:06
Here I have string like this "1322485986.672901000"
, data type is string which represents unixtime. I want to convert it into datetime in Python.
The way I used is date = datetime.fromtimestamp(float(row[0]))
row[0] represents value like this 1322485986.672901000
, since I have several rows. The convert result is wrong, it only calculates the digits before the dot. So the date after conversion is like 2011-11-28 13:53:23.6729
I think the problem is float(row[0])
, but I don't know how to solve this problem, or does anyone know how to convert unixtime to datetime in a better way?
Many thanks!
Now the result I got is 2011-11-28 14:13:06.672901
But when using an online converter, the result is 2011-11-28 13:13:06
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当前结果中一小时的差异可能是时区应用的结果。
如果需要,您可以提供明确的时区 - tzinfo 对象上的文档< /a> 可以引导您直接创建时区对象,创建后,您只需传入参数即可:
此代码打印 2011-11-28对我来说是 13:13:06.672901+00:00。
Your one-hour difference in the current result is likely the consequence of time zone application.
You can supply an explicit time zone if you need to -- the docs on tzinfo objects can steer you straight on creating your time zone object, and once it's created, you just pass in the argument:
This code prints 2011-11-28 13:13:06.672901+00:00 for me.
产量:
Yields: