未转换的数据仍然存在:Python 中的 .387000

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29216208/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 04:12:57  来源:igfitidea点击:

unconverted data remains: .387000 in Python

pythonstrftimestrptime

提问by TheBigOnion

I have a datetime that is a string I read from a text file. I want to trim the extra milliseconds off of it, but I want to convert it to a datetime variable first. This is because it may be in different formats depending on what is in the text file. How ever, everything I have tried wants me to already know the format. Please take a look at what I am trying here:

我有一个日期时间,它是我从文本文件中读取的字符串。我想修剪掉额外的毫秒数,但我想先将它转换为日期时间变量。这是因为根据文本文件中的内容,它可能采用不同的格式。然而,我尝试过的一切都希望我已经知道格式。请看一下我在这里尝试的内容:

import time, datetime

mytime = "2015-02-16 10:36:41.387000"
myTime = time.strptime(mytime, "%Y-%m-%d %H:%M:%S.%f")

myFormat = "%Y-%m-%d %H:%M:%S"

print datetime.datetime.fromtimestamp(time.mktime(time.strptime(mytime, myFormat)))

But this gives me this error:

但这给了我这个错误:

File "python", line 10, in <module>
ValueError: unconverted data remains: .387000`

Can someone please tell me how to do a normal datetime format? In other languages I can pass in various formats and then set it to a new format without a problem.

有人可以告诉我如何做一个正常的日期时间格式吗?在其他语言中,我可以传入各种格式,然后毫无问题地将其设置为新格式​​。

采纳答案by Selcuk

You are doing it backwards. Try this:

你是在倒退。尝试这个:

from datetime import datetime

mytime = "2015-02-16 10:36:41.387000"
myTime = datetime.strptime(mytime, "%Y-%m-%d %H:%M:%S.%f")

myFormat = "%Y-%m-%d %H:%M:%S"

print "Original", myTime
print "New", myTime.strftime(myFormat)

result:

结果:

Original 2015-02-16 10:36:41.387000
New 2015-02-16 10:36:41

回答by Mauro Baraldi

You forgot to reference microseconds in myFormat

你忘了引用微秒 myFormat

myFormat = "%Y-%m-%d %H:%M:%S.%f"

Anyway, you can convert it with less steps

无论如何,你可以用更少的步骤转换它

from datetime import datetime

mytime = "2015-02-16 10:36:41.387000"
full = "%Y-%m-%d %H:%M:%S.%f"
myTime = datetime.strptime(mytime, full)
>>> datetime.datetime(2015, 2, 16, 10, 36, 41, 387000)

Here mytimeis in datetimeobject. If you want print without microseconds, use the strftime

这里mytimedatetime对象。如果您想在没有微秒的情况下打印,请使用strftime

myfmt = "%Y-%m-%d %H:%M:%S"
print datetime.strptime(mytime, full).strftime(myfmt)
>>> 2015-02-16 10:36:41

回答by Johnny Gasyna

If nothing else is changing, i.e. the format, except that you are dropping the micro seconds at the end, you don't really need to call strftime. You can just substring it:

如果没有其他任何变化,即格式,除了您在最后删除微秒之外,您实际上不需要调用 strftime。您可以将其子字符串化:

mytime = "2015-02-16 10:36:41.387000"
mytime = mytime[:-7]    
print mytime

result:

结果:

2015-02-16 10:36:41

Disclaimer: This is only be applicable if you do not to re-format or rearrange anything, and only if you are trying to drop the last bit.

免责声明:这仅适用于您不重新格式化或重新排列任何内容的情况,并且仅适用于您尝试删除最后一位的情况。

回答by sailfish009

alternative solution is using split():

替代解决方案是使用 split():

# mytime is str(timestamp)
datetime.datetime.strptime(mytime.split('.')[0], '%Y-%m-%d %H:%M:%S')