[Tutor] stx, etx (\x02, \x03)

richard kappler richkappler at gmail.com
Mon Sep 28 15:04:39 CEST 2015


>
> Hi Richard,
>
> Just to check: what operating system are you running your program in?
> Also, what version of Python?
>

Hi Danny, using Linux and Python 2.7

>
>
>
> ##############################
> with open('input/test.xml', 'rU') as f1: ...
> ##############################
>
>
> Question: can you explain why the program is opening 'mod1.xml' in 'a'
> append mode?  Why not in 'w' write mode?
>
>
> This may be important because multiple runs of the program will append
> to the end of the file, so if you inspect the output file, you may be
> confusing the output of prior runs of your program.  Also, it's likely
> that the output file will be malformed, since there should just be one
> XML document per file.  In summary: opening the output file in append
> mode looks a bit dubious here.
>
>
>
> To your other question:
>
> > What am I missing?
>
> Likely, the lines being returned from f1 still have a line terminator
> at the end.  You'll want to interpose the '\x03' right before the line
> terminator.  Mark Laurence's suggestion to use:
>
>     s = '\x02' + line[:-1] + '\x03\n'
>
> looks ok to me.
>

Actually, you solved it, but it required both

with open('input/test.xml', 'rU') as f1:...

and

s = 'x02' + line[:-1] + 'x03\n'

Either of the above alone do not resolve the issue, but both together do.

As far as the write vs append, I understand your concerns but that is for
testing during scripting only. Ultimately the script outputs to a socket on
another machine, not to a file, so that gets commented out. The append is
because I am using a 100 line test file with a

for line in f1:

statement in it, and I want the results for each line appended to the
output file. I delete the results file before each run.

Thanks for all the help!

regards, Richard



-- 

All internal models of the world are approximate. ~ Sebastian Thrun


More information about the Tutor mailing list