[Tutor] formatting xml (again)

richard kappler richkappler at gmail.com
Tue Dec 27 15:39:36 EST 2016


I was actually working somewhat in that direction while I waited. I had in
mind to use something along the lines of:


stx = '\x02'
etx = '\x03'
line1 = ""

with open('original.log', 'r') as f1:
   with open('new.log', 'w') as f2:
        for line in f1:
            if stx in line:
                line1 = line1 + line
            if not stx in line:
                if not etx in line:
                    line1 = line1 + line
            if etx in line:
                line1 = line1 + line + '\n'
                f2.write(line1)
                line1 = ""


but that didn't work. It neither broke each line on etx (multiple events
with stx and etx on one line) nor did it concatenate the multi-line events.

On Tue, Dec 27, 2016 at 3:25 PM, David Rock <david at graniteweb.com> wrote:

> * richard kappler <richkappler at gmail.com> [2016-12-27 14:44]:
> >
> > I have tried to feed this raw into our other app (Splunk) and the app
> reads
> > each line (gedit numbered line) as an event. I want everything in between
> > each stx and etx to be one event.
> >
> > I have tried:
> >
> > #####################################
> > with open("original.log", 'r') as f1:
> >     with open("new.log", 'a') as f2:
> >         for line in f1:
> >             line2 = line.replace("\n", "")
> >             f2.write(line2)
> > ######################################
> >
> > Now this obviously doesn't work because, as stated above, each tag and
> > datum in the example above from lines 2 to 7 is on a different line, so
> > python is doing exactly as I tell it, it's stripping the \n and then
> > printing the line, but not concatenating everything between stx and etx
> on
> > one line, which is what I want it to do.
> >
> > What I'm trying to do is collapse the 'expanded lines' between stx and
> etx
> > to one line, but I just can't wrap my head around how to do it. Or to
> put,
> > and do, it another way, how do I read each line from the original file,
> but
> > write it to another file so that everything from stx to etx, including
> stx
> > and etx, are on one line in the file?
>
> Concatinate all your lines into a single output variable first, then
> write that to your log
>
> Pseudocode (ie, this won't run), but this should give you the idea
> (defining the parts to loop will be the challenge you will have to
> define for yourself).
>
> with open("original.log", 'r') as f1:
>     with open("new.log", 'a') as f2:
>         output = ""
>         for line in f1:
>             if between [x02] and [x03]:
>                 output =+ line.strip()
>             else:
>                 f2.write(output)
>                 output = ""
>
> Basically, you need to loop over everything between your markers and put
> them in a single entry, then send that one entry all at once instead of
> piecemeal.
>
> I can come up with something a little more complete if you still need
> more help.
>
> --
> David Rock
> david at graniteweb.com
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>


More information about the Tutor mailing list