Python Forum
analyzing a text and copy lines
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
analyzing a text and copy lines
#5
Something like this?
from pathlib import Path


class Security:
    def __init__(self, match_value):
        '''
        Note directory structure:
        put your selected file in threats directory.

        YourSourceDir/
            data/
                threats/
                    EmergingThreats.txt
            src/
                thisProgram.py
        :param match_value: The value you want to select and write to output file
        '''
        self.homepath = Path('.')
        self.rootpath = self.homepath / '..'
        self.datapath = self.rootpath / 'data'
        self.datapath.mkdir(exist_ok=True)
        self.threatpath = self.datapath / 'threats'
        self.threatpath.mkdir(exist_ok=True)

        self.emthreat_file = self.threatpath / 'EmergingThreats.txt'
        self.selections_out = self.threatpath / 'SelectedThreats.txt'
        self.select_output(match_value)

    def select_output(self, match_value):
        with self.emthreat_file.open('r') as f, self.selections_out.open('w') as f1:
            for line in f:
                line = line.strip()
                if line.startswith('#') or len(line) == 0:
                    continue
                cidx = line.index('classtype')
                classtype = line[cidx:]
                cidx = classtype.index(';')
                classtype = classtype[:cidx]
                ctype = classtype.split(':')
                classtype = ctype[1]

                if classtype == match_value:
                    f1.write('{}\n'.format(line))

def testit():
    # change classtype you want selected here
    Security('misc-attack')

if __name__ == '__main__':
    testit()
You can modify this for your exact requirements
Reply


Messages In This Thread
analyzing a text and copy lines - by marta - Apr-17-2018, 05:22 AM
RE: analyzing a text and copy lines - by Larz60+ - Apr-17-2018, 05:43 AM
RE: analyzing a text and copy lines - by Larz60+ - Apr-17-2018, 06:12 AM
RE: analyzing a text and copy lines - by marta - Apr-17-2018, 07:04 AM
RE: analyzing a text and copy lines - by Larz60+ - Apr-17-2018, 09:02 AM
RE: analyzing a text and copy lines - by yesterday - Apr-17-2018, 09:17 AM
RE: analyzing a text and copy lines - by Larz60+ - Jun-15-2018, 09:11 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Why is the copy method name in python list copy and not `__copy__`? YouHoGeon 2 349 Apr-04-2024, 01:18 AM
Last Post: YouHoGeon
  Editing text between two string from different lines Paqqno 1 1,360 Apr-06-2022, 10:34 PM
Last Post: BashBedlam
  Extracting Specific Lines from text file based on content. jokerfmj 8 3,184 Mar-28-2022, 03:38 PM
Last Post: snippsat
  raspberry use scrolling text two lines together fishbone 0 1,493 Sep-06-2021, 03:24 AM
Last Post: fishbone
  More elegant way to remove time from text lines. Pedroski55 6 4,070 Apr-25-2021, 03:18 PM
Last Post: perfringo
  how to connect mysql from txt 1 line goes good but not all lines in text kingceasarr 4 2,967 Mar-24-2021, 05:45 AM
Last Post: buran
  Iterate 2 large text files across lines and replace lines in second file medatib531 13 6,151 Aug-10-2020, 11:01 PM
Last Post: medatib531
  copy content of text file with three delimiter into excel sheet vinaykumar 0 2,411 Jul-12-2020, 01:27 PM
Last Post: vinaykumar
  Read Multiples Text Files get specific lines based criteria zinho 5 3,243 May-19-2020, 12:30 PM
Last Post: zinho
  How to detect the text above lines using OpenCV in Python pframe 0 2,551 Apr-14-2020, 09:53 AM
Last Post: pframe

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020