[Tutor] Re: test if file is not ascii

Alan Gauld alan.gauld at blueyonder.co.uk
Sat Nov 1 12:48:14 EST 2003


> for letter in line:
>    char = ord(letter)
>    if char < 20:

It should be possible to write a regex for all the letters 
below ASCII 20. Checking if the regex exists in the line 
is almost certainly faster than checking each character 
using Python.

Something like:
regex  = "["
for n in range(20): regex = regex+chr(n)
regex = regex+"]"
re.compile(regex)

> sys.stderr.write('File contains illegal characters.\n')
> return 101

It might be nicer to raise an exception. 
Certainly more Pythonic.

Alan G.



More information about the Tutor mailing list