PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, August 14, 2022

[FIXED] how to skip blocks of text when writing file in python

 August 14, 2022     file, input, output, python     No comments   

Issue

Is it possible to use python to skip blocks of text when writing a file from another file?

For example lets say the input file is:

This is the file I would like to write this line
I would like to skip this line
and this one...
and this one...
and this one...
but I want to write this one
and this one...

How can I write a script that allows me to skip certain lines that differ in content and size which resumes writing the lines to another file once it recognizes a certain line?

My code reads through the lines, doesn't write duplicate lines and performs some operation on the line by using dictionaries and regex.


Solution

def is_wanted(line):
    #
    # You have to define this!
    #
    # return True to keep the line, or False to discard it

def copy_some_lines(infname, outfname, wanted_fn=is_wanted):
    with open(infname) as inf, open(outfname, "w") as outf:
        outf.writelines(line for line in inf if wanted_fn(line))

copy_some_lines("file_a.txt", "some_of_a.txt")

In order to extend this to multi-line blocks, you can implement a finite state machine like

enter image description here

which would turn into something like

class BlockState:
    GOOD_BLOCK = True
    BAD_BLOCK = False

    def __init__(self):
        self.state = self.GOOD_BLOCK

    def is_bad(self, line):
        # *** Implement this! ***
        # return True if line is bad

    def is_good(self, line):
        # *** Implement this! ***
        # return True if line is good

    def __call__(self, line):
        if self.state == self.GOOD_BLOCK:
            if self.is_bad(line):
                self.state = self.BAD_BLOCK
        else:
            if self.is_good(line):
                self.state = self.GOOD_BLOCK
        return self.state

then

copy_some_lines("file_a.txt", "some_of_a.txt", BlockState())


Answered By - Hugh Bothwell
Answer Checked By - Clifford M. (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing