I am able to parse strings containing date/time with time.strptime
>>> import time >>> time.strptime("30/03/09 16:31:32", "%d/%m/%y %H:%M:%S") (2009, 3, 30, 16, 31, 32, 0, 89, -1)
How can I parse a time string that contains milliseconds?
>>> time.strptime("30/03/09 16:31:32.123", "%d/%m/%y %H:%M:%S") Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/lib/python2.5/_strptime.py", line 333, in strptime data_string[found.end():]) ValueError: unconverted data remains: .123
Python 2.6 added a new strftime/strptime macro
%f. The docs are a bit misleading as they only mention microseconds, but
%f actually parses any decimal fraction of seconds with up to 6 digits, meaning it also works for milliseconds or even centiseconds or deciseconds.
time.strptime("30/03/09 16:31:32.123", "%d/%m/%y %H:%M:%S.%f")
However, time.struct_time doesn"t actually store milliseconds/microseconds. You"re better off using
datetime, like this:
>>> from datetime import datetime >>> a = datetime.strptime("30/03/09 16:31:32.123", "%d/%m/%y %H:%M:%S.%f") >>> a.microsecond 123000
As you can see,
.123 is correctly interpreted as
123 000 microseconds.
Scientific progress has increasingly become reliant on large-scale data collection and analysis methodologies. The same is true for the advanced use of computing in business, government, and other are...
Coding for Kids: Python - Learn to Code with 50 Awesome Games and Activities. Learning to code isn't as difficult as it sounds, you just have to get started! Coding for Kids: Python gets kids start...
While there is no arguing about the staying power of the cloud model and the benefits it can bring to any organization or government, mainstream adoption depends on several key variables falling into ...
Efficiently perform data collection, wrangling, analysis, and visualization using Python. Recent advancements in computing and artificial intelligence have completely changed the way we understand ...