Python Forum
Help with python code to search string in one file & replace with line in other file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with python code to search string in one file & replace with line in other file
#11
I'm not going to get to this tonight, but will pick it up first thing in the AM.
I'll post the code if you want to play with it before then.
It's now replacing all of the headers, and not the data, but it's reusing the same header lines from file2.
I think I know why, but need to spend some time with my wife.
I'll pick it up latter tonight or in the A.M. (EST)

I named the program MyParse.py, you can call it whatever you like, but just change the run command which is:
MyParse.py -i 'File1.txt' -b 'File2.txt' -o 'Fileout.txt'
here's the code:
# Replace header inoriginal file header with header in header file, writing output to outputfile
# Larz60+
from pathlib import Path
import argparse

class SwapHeaders:
    def __init__(self, origfile=None, headerfile=None, outfile=None):
        self.home = Path('.')
        self.data = self.home / 'data'
        self.original_file = self.data / origfile
        self.header_file = self.data / headerfile
        self.out_file = self.data / outfile

        with self.header_file.open() as fh:
            self.header_data = fh.readlines()

        self.orig = self.original_file.open()
        self.fo = self.out_file.open('w')

    def close_files(self):
        self.orig.close()
        self.fo.close()

    def get_replacement_header(self, match):
        retrec = None
        for line in self.header_data:
            if not line.startswith('>'):
                continue
            if match in line:
                retrec = line
                break
        return retrec

    def read_orig_record(self):
        """
        original file record read
        :return: data or False
        """
        while True:
            data = self.orig.readline()
            if not data:
                break
            yield data

    def make_new_file(self):
        with self.out_file.open('w') as fo:
            for orig in self.read_orig_record():
                if orig.startswith('>'):
                    match = orig[1:]
                    x = match.rfind('.')
                    if x:
                        match = match[:x]
                    new = self.get_replacement_header(match)
                    if new is not None:
                        fo.write(new)
                    else:
                        fo.write(orig)
                else:
                    fo.write(orig)

def main():
    # Typical command line call python MyParse.py -i 'File1.txt' -b 'File2.txt' -o 'Fileout.txt'
    parser = argparse.ArgumentParser()
    parser.add_argument("-i", "--ifile",
                        dest='original_filename',
                        help="Filename where headers are to be replaced",
                        action="store")

    parser.add_argument("-b", "--bfile",
                        dest='replace_original_filename',
                        help="Filename containing body",
                        action="store")

    parser.add_argument("-o", "--ofile",
                        dest='out_filename',
                        help="Output filename",
                        action="store")

    args = parser.parse_args()
    original_filename = args.original_filename

    replace_original_filename = args.replace_original_filename

    out_filename = args.out_filename

    sh = SwapHeaders(origfile=original_filename, headerfile=replace_original_filename, outfile=out_filename)
    sh.make_new_file()
    sh.close_files()

if __name__ == '__main__':
    main()
Reply


Messages In This Thread
RE: Help with python code to search string in one file & replace with line in other file - by Larz60+ - Dec-17-2017, 03:27 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  python coding to run file NSR115 1 217 Jun-18-2024, 11:05 AM
Last Post: Kajalishu
  "[Errno 2] No such file or directory" (.py file) IbrahimBennani 13 815 Jun-17-2024, 12:26 AM
Last Post: AdamHensley
  Printing the code line number arbiel 2 286 Jun-15-2024, 07:37 PM
Last Post: arbiel
  Replace values in Yaml file with value in dictionary PelleH 0 168 Jun-12-2024, 02:40 PM
Last Post: PelleH
  Cannot get cmd to print Python file Schauster 11 733 May-16-2024, 04:40 PM
Last Post: xMaxrayx
  Matching string from a file tester_V 5 656 Mar-05-2024, 05:46 AM
Last Post: Danishhafeez
  Python openyxl not updating Excel file MrBean12 1 533 Mar-03-2024, 12:16 AM
Last Post: MrBean12
  Python logging RotatingFileHandler writes to random file after the first log rotation rawatg 0 545 Feb-15-2024, 11:15 AM
Last Post: rawatg
  Unable to understand the meaning of the line of code. jahuja73 0 448 Jan-23-2024, 05:09 AM
Last Post: jahuja73
  connect sql by python using txt. file dawid294 2 602 Jan-12-2024, 08:54 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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