How can I search a range of lines in python?

Jamie

I would like to search through a range of lines in a date ordered log file between two dates. If I were at the command line, sed would come handy with:

sed -rn '/03.Nov.2012/,/12.Oct.2013/s/search key/search key/p' my.log

The above would only display lines between the 3 November, 2012 and 12 of October, 2013 that contain the string "search key".

Is there a light weight way I can do this in python?

I could build a single RE for the above , but it would be nightmarish.

The best I can come up with is this:

#!/usr/bin/python

start_date = "03/Nov/2012"
end_date = "12/Oct/2013"

start = False

try:
    with open("my.log",'r') as log:
        for line in log:
            if start:
                if end_date in line:
                    break
            else:
                if start_date in line:
                    start = True
                else:
                    continue
            if search_key in line:
                print line

except IOError, e:
    print '<p>Log file not found.'

But this strikes me as not 'pythonic'.

One can assume that search date limits will be found in the log file.

Jon Clements

Using itertools and a generator is one way:

from itertools import takewhile, dropwhile

with open('logfile') as fin:
    start = dropwhile(lambda L: '03.Nov.2012' not in L, fin)
    until = takewhile(lambda L: '12.Oct.2013' not in L, start)
    query = (line for line in until if 'search string' in line)
    for line in query:
        pass # do something

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

how can I save the output of a search for files matching *.txt to a variable?

来自分类Dev

How can I use sed to find the lines that start with "X", then find and replace inside this line using Perl?

来自分类Dev

can I do this binary search using the STL?

来自分类Dev

How can I reuse exception handling code for multiple functions in Python?

来自分类Dev

How can I use urllib.request.urlretrieve with python 2.7

来自分类Dev

How can I associate xml text with a preceding empty element in Python?

来自分类Dev

How can I change the value of a morsel of a cookie in a python CookieJar?

来自分类Dev

Can I fix Conceal highlight group background in highlighted lines?

来自分类Dev

how to output all the lines into python console in vim?

来自分类Dev

How to delete multiple lines in a python console window?

来自分类Dev

How can search same string with PHP

来自分类Dev

Search Value from range of an array

来自分类Dev

Python: how to search for a substring in a set the fast way?

来自分类Dev

How can I cancel an ngEvent?

来自分类Dev

How can I return a function?

来自分类Dev

How can I write a non-stop crawler with python and run it on a server?

来自分类Dev

In Python 3 how can I patch a function inside a module for unit testing?

来自分类Dev

How can I read an image from an Internet URL in Python cv2, scikit image and mahotas?

来自分类Dev

How can I send data from a TCP Python server to a client and vice-versa?

来自分类Dev

How can Convert traditional ADO.Net search to Linq Entity

来自分类Dev

How do I get Ruby to search for a pattern on the tail of a local file?

来自分类Dev

Why can't I split this python list?

来自分类Dev

如何在``for i in range(x)''中将我附加到python中的标签?

来自分类Dev

How can I create a histogram in R?

来自分类Dev

How can I select with column of lists

来自分类Dev

How can I extract text from images?

来自分类Dev

How can I pipe input to a process?

来自分类Dev

How i can onUpgrade me database in Android

来自分类Dev

How can I check for reference equality in Perl?

Related 相关文章

  1. 1

    how can I save the output of a search for files matching *.txt to a variable?

  2. 2

    How can I use sed to find the lines that start with "X", then find and replace inside this line using Perl?

  3. 3

    can I do this binary search using the STL?

  4. 4

    How can I reuse exception handling code for multiple functions in Python?

  5. 5

    How can I use urllib.request.urlretrieve with python 2.7

  6. 6

    How can I associate xml text with a preceding empty element in Python?

  7. 7

    How can I change the value of a morsel of a cookie in a python CookieJar?

  8. 8

    Can I fix Conceal highlight group background in highlighted lines?

  9. 9

    how to output all the lines into python console in vim?

  10. 10

    How to delete multiple lines in a python console window?

  11. 11

    How can search same string with PHP

  12. 12

    Search Value from range of an array

  13. 13

    Python: how to search for a substring in a set the fast way?

  14. 14

    How can I cancel an ngEvent?

  15. 15

    How can I return a function?

  16. 16

    How can I write a non-stop crawler with python and run it on a server?

  17. 17

    In Python 3 how can I patch a function inside a module for unit testing?

  18. 18

    How can I read an image from an Internet URL in Python cv2, scikit image and mahotas?

  19. 19

    How can I send data from a TCP Python server to a client and vice-versa?

  20. 20

    How can Convert traditional ADO.Net search to Linq Entity

  21. 21

    How do I get Ruby to search for a pattern on the tail of a local file?

  22. 22

    Why can't I split this python list?

  23. 23

    如何在``for i in range(x)''中将我附加到python中的标签?

  24. 24

    How can I create a histogram in R?

  25. 25

    How can I select with column of lists

  26. 26

    How can I extract text from images?

  27. 27

    How can I pipe input to a process?

  28. 28

    How i can onUpgrade me database in Android

  29. 29

    How can I check for reference equality in Perl?

热门标签

归档