Python Forum
txt-file: read and append missing data
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
txt-file: read and append missing data
#2
You need to parse the input one way or another. Here is a simple parser. It reads the input lines and for each line it calls a method line_event(). Initially this method simply outputs the line to the output file. If it meets a line starting with .CURVE, it changes the method to line_event_curve_header() which outputs the lines until it meets a line ..NE. In the meanwhile it detects if it meets a line starting with ...A_Z or ...B_Z. After the last line of the header, if ...A_Z or ...B_Z has not been found it changes the method to line_event_curve_body() which appends 0.00 to the lines until it meets a line starting a new object.

Here is the (untested) code
import re

class Parser:
    object_pattern = re.compile(r'^[.](CURVE|POINT|TEXT)')
    
    def __init__(self, infile, outfile):
        self.infile = infile
        self.outfile = outfile
        self.line_event = self.line_event_base
        
    def run(self):
        for line in self.infile:
            self.line_event(line)
        self.outfile.flush()
        
    def line_event_base(self, line):
        if line.startswith('.CURVE'):
            self.start_curve(line)
        else:
            self.outfile.write(line)
            
    def line_event_curve_header(self, line):
        if line.startswith('...A_Z') or line.startswith('...B_Z'):
            self.seen_ab_z = True
            self.output.write(line)
        elif line.rstrip('\n') == '..NE':
            if self.seen_ab_z:
                self.line_event = self.line_event_base
                self.output.write(line)
            else:
                self.output.write('...A_Z 0.00\n...B_Z 0.00\n..NEZ\n')
                self.line_event = self.line_event_curve_body
        else:
            self.output.write(line)
            
    def line_event_curve_body(self, line):
        if self.object_pattern.match(line):
            if line.startswith('.CURVE'):
                self.start_curve(line)
            else:
                self.line_event = self.line_event_base
                self.outfile.write(line)
        else:
            self.outfile.write(line.rstrip('\n'))
            self.outfile.write(' 0.00\n')
                
    def start_curve(self, line):
        self.line_event = self.line_event_curve_header
        self.seen_ab_z = False
        self.line_event(line)

if __name__ == '__main__':
    with open('C:/Users/sufi/Desktop/info.txt','r') as infile,\
            open('C:/Users/sufi/Desktop/info_edit.txt','w') as outfile:
        Parser(infile, outfile).run()
Reply


Messages In This Thread
txt-file: read and append missing data - by sufi - Dec-06-2019, 03:46 PM
RE: txt-file: read and append missing data - by Gribouillis - Dec-07-2019, 08:12 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Connecting to Remote Server to read contents of a file ChaitanyaSharma 1 392 May-03-2024, 07:23 AM
Last Post: Pedroski55
  Help with to check an Input list data with a data read from an external source sacharyya 3 603 Mar-09-2024, 12:33 PM
Last Post: Pedroski55
  Recommended way to read/create PDF file? Winfried 3 3,076 Nov-26-2023, 07:51 AM
Last Post: Pedroski55
  python Read each xlsx file and write it into csv with pipe delimiter mg24 4 1,770 Nov-09-2023, 10:56 AM
Last Post: mg24
  read file txt on my pc to telegram bot api Tupa 0 1,247 Jul-06-2023, 01:52 AM
Last Post: Tupa
  parse/read from file seperated by dots giovanne 5 1,235 Jun-26-2023, 12:26 PM
Last Post: DeaD_EyE
  Formatting a date time string read from a csv file DosAtPython 5 1,589 Jun-19-2023, 02:12 PM
Last Post: DosAtPython
  How do I read and write a binary file in Python? blackears 6 7,942 Jun-06-2023, 06:37 PM
Last Post: rajeshgk
  Read csv file with inconsistent delimiter gracenz 2 1,335 Mar-27-2023, 08:59 PM
Last Post: deanhystad
Question How to append integers from file to list? Milan 8 1,631 Mar-11-2023, 10:59 PM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

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