Hack Forums
[Python] Script that extras lines from .txt file based on character count and phrases - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [Python] Script that extras lines from .txt file based on character count and phrases (/showthread.php?tid=5977846)



Script that extras lines from .txt file based on character count and phrases - Cabgun Jock - Jun 8, 2019

I have a large .txt file that I need to scan through to extrapolate certain lines of text from. I want to extract all lines that contain X amount of characters, and a certain phrase. How can I go about doing this?


RE: Script that extras lines from .txt file based on character count and phrases - michalmonday - Jun 8, 2019

Code
with open('data.txt') as f:
   for line in f.readlines():
       if len(line) > 5 and 'some_word' in line:
           print(line)



RE: Script that extras lines from .txt file based on character count and phrases - vorticalbox - Jun 9, 2019

(Jun 8, 2019 01:36 PM)michalmonday Wrote:  
Code
with open('data.txt') as f:
   for line in f.readlines():
       if len(line) > 5 and 'some_word' in line:
           print(line)

It would probably be better to use some sort of regular expression for this.


RE: Script that extras lines from .txt file based on character count and phrases - HFDone - Jun 10, 2019

(Jun 9, 2019 02:12 PM)vorticalbox Wrote:  It would probably be better to use some sort of regular expression for this.

dont think so as he want the lines not the regex
the example posted is the simple way to do it