[Tutor] How to print lines within two timestamps
Alan Gauld
alan.gauld at yahoo.co.uk
Sat Oct 27 08:21:49 EDT 2018
On 27/10/2018 10:28, Asad wrote:
> C:\Python27\python.exe D:/QI/test_qopatch.py
> Traceback (most recent call last):
> 2018-10-22 10:21:15
> File "D:/QI/test_qopatch.py", line 32, in <module>
> 2018-10-22 10:21:25
> newtime = datetime.datetime.strptime ( a[0], '%m/%d/%y %H:%M:%S' )
> ['04/26/18 06:11:52']
> 2018-04-26 06:11:52
> []
> IndexError: list index out of range
Notice that the 2nd last line says you have an empty list.
So a[0] will produce an index error since there is no element 0.
You need to check for an empty list before the assignment.
(Or catch the index error in a try/except)
Notice the help for re.findall() says
############################
findall(pattern, string, flags=0)
Return a list of all non-overlapping matches in the string.
If one or more groups are present in the pattern, return a
list of groups; this will be a list of tuples if the pattern
has more than one group.
Empty matches are included in the result.
###########################
See the last line?
BTW Please delete the digest items that are not relevant. These
messages go to the full 400+ members of the list and many pay
by the byte. They don't want to see stuff they have
already received unless it's relevant.
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos
More information about the Tutor
mailing list