[Tutor] file.read..... Abort Problem (fwd)

Kent Johnson kent37 at tds.net
Fri Oct 21 02:02:54 CEST 2005


> From: Tomas Markus <tomas.markus at gmail.com>
> Just to let you know my ressults (+ one tiny question :-):
> I did a bit of investigation and narrowed the "invalid" characters group to
> jus one the hex 1a. My code now looks quite small:
> 
> fpath = 'c:\\pytemp\\bafir550.edi'
> fl = file(fpath, 'rb')
> print 'Checking file %s' % fpath
> x=0
> for line in fl.readlines():
> x=x+1
> if "" in line:
> print 'Not allowed chars at line %s --- %s' % (x,line),
> print "The check finished on line %s which was %s" % (x,line)
> 
> The only proble is tha in the if clause there actually is the hex 1a
> character. 

You don't have to type an actual 1a character to get it into a Python string. You can use special character escapes instead. To put a hex value into a string, use the escape \x followed by the two hex digits. So your test can be
  if "\x1a" in line:

This way there is no actual 1a character in your program, but it will still do what you want.

Kent



More information about the Tutor mailing list