[Tutor] formatting xml (again)

David Rock david at graniteweb.com
Tue Dec 27 15:25:16 EST 2016


* 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


More information about the Tutor mailing list