简体   繁体   English

Python将字符串转换为datetime以与datetime对象进行比较

[英]Python convert string to datetime for comparison to datetime object

I have a string lfile with a datetime in it ( type(lfile) gives <type 'str'> ) and a Python datetime object wfile . 我有一个带有日期时间的字符串lfiletype(lfile)给出<type 'str'> type(lfile) <type 'str'> )和一个Python datetime对象wfile Here is the code: 这是代码:

import os, datetime
lfile = '2005-08-22_11:05:45.000000000'
time_w = os.path.getmtime('{}\\{}.py' .format('C:\Temp_Readouts\RtFyar','TempReads.csv'))
wfile = datetime.datetime.fromtimestamp(time_w)

wfile contains this 2006-11-30 19:08:06.531328 and repr(wfile) gives: wfile包含这个2006-11-30 19:08:06.531328repr(wfile)给出:

datetime.datetime(2006, 11, 30, 19, 8, 6, 531328)

Problem: 问题:

I need to: 我需要:

  1. convert lfile into a Python datetime object lfile转换为Python datetime对象
  2. compare lfile to wfile and determine which datetime is more recent lfilewfile进行比较并确定哪个日期时间更新

For 1.: 1:

I am only able to get a partial solution using strptime as per here . 我只能在这里使用strptime获得部分解决方案。 Here is what I tried: 这是我尝试过的:

lfile = datetime.datetime.strptime(linx_file_dtime, '%Y-%m-%d_%H:%M:%S')

The output is: 输出是:

`ValueError: unconverted data remains: .000`

Question 1 问题1

It seems that strptime() cannot handle the nano seconds. 似乎strptime() 无法处理纳秒。 How do I tell strptime() to ignore the last 3 zeros? 我怎么告诉strptime()忽略最后3个零?

For 2.: 2:

When I use type(wfile) I get <type 'datetime.datetime'> . 当我使用type(wfile)我得到<type 'datetime.datetime'> type(wfile) <type 'datetime.datetime'> If both wfile and lfile are Python datetime objects (ie if step 1. is successful), then would this work?: 如果wfilelfile都是Python datetime对象(即如果步骤1成功),那么这会有效吗?:

if wtime < ltime:
    print 'Linux file created after Windows file'
else:
    print 'Windows file created after Linux file'

Question 2 问题2

Or is there some other way in which Python can compare datetime objects to determine which of the two occurred after the other? 或者是否有其他方法可以让Python比较日期时间对象以确定两者中哪一个发生在另一个之后?

Question 1 问题1

Python handles microseconds, not nano seconds. Python处理微秒,而不是纳秒。 You can strip the last three characters of the time to convert it to microseconds and then add .%f to the end: 您可以删除时间的最后三个字符以将其转换为微秒,然后将.%f添加到结尾:

lfile = datetime.datetime.strptime(linx_file_dtime[:-3], '%Y-%m-%d_%H:%M:%S.%f')

Question 2 问题2

Yes, comparison works: 是的,比较有效:

if wtime < ltime:
    ...

That's right, strptime() does not handle nanoseconds. 没错, strptime()不会处理纳秒。 The accepted answer in the question that you linked to offers an option: strip off the last 3 digits and then parse with .%f appended to the format string. 您链接的问题中接受的答案提供了一个选项:去除最后3位数字,然后使用.%f解析格式字符串。

Another option is to use dateutil.parser.parse() : 另一种选择是使用dateutil.parser.parse()

>>> from dateutil.parser import parse
>>> parse('2005-08-22_11:05:45.123456789', fuzzy=True)
datetime.datetime(2005, 8, 22, 11, 5, 45, 123456)

fuzzy=True is required to overlook the unsupported underscore between date and time components. fuzzy=True才能忽略日期和时间组件之间不受支持的下划线。 Because datetime objects do not support nanoseconds, the last 3 digits vanish, leaving microsecond accuracy. 因为datetime对象不支持纳秒,所以最后3位数字消失,留下微秒精度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM