0

Friends I need to search for some strings like 'info stat, waiting, connecting, connection pending' in the source txt file those lines which has the strings need to be extracted and saved to a new file.

import re
import os
import sys

file = open("C:\Users\Administrator\Desktop\logs test\source.txt", 'rU')
script = file.readlines() 
file.close() 
for line in script:
 re.search(r'book.info', script, re.I)

after searching how to get the lines that matches the string to a file. Thanks in advance

2
Contributors
2
Replies
3
Views
6 Years
Discussion Span
Last Post by pyTony
0

Adapt my code snippet to do in test for lines instead of whole file:
http://www.daniweb.com/software-development/python/code/316585

More simple finding word in file, showing lines containing word:

fname = 'alice.txt'
find = ('cat', 'queen')
with open(fname) as infile:
    for line_no, line in enumerate(infile, 1):
        lline = line.lower()
        if any(word.lower() in lline for word in find):
            print('%4i: %s' % (line_no, line.rstrip()))

Edited by pyTony: n/a

0

BTW your original code line 5 should have r"" quotes (maybe works by chance, but what if file or directory name starts with 'n'?).

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.