python - Navigating Callable-Iterators -
I want to use regular expressions to extract information from some chat logs. The format of the strings has been parsed 03:22:32 PM
times = [] messages = again .compile (r '(? P>; time & gt; ..: ..: .. ..). *? & Lt; b & Gt; (? P & lt; usrname & gt;. *?): & Lt; / b & gt; '). Search) for results in message: times.append (result.group ('time')) Is there a better and effective way of doing this? Thanks for the help.
An Iterator is an object with a next method. Every time you call it, it returns the next item in a collection. If you need to arbitrarily index, then you have to convert it to a list. Instead: For results in the message
: times.append (result.group ('time'))
You can say: <
This is very much the same thing. However, I should warn you that doing this for a large result set will be very slow and will eat a bunch of memory. Thus, you should not do this if you do not need random access. If an untrusted user enters, it will determine how many results will appear, you can limit the number of things they can enter.
Edit : I have just seen that my previous answer was not the same as the snippet you posted, so I updated it.
Comments
Post a Comment