Checking for binary data in a string

Grant Edwards invalid at invalid
Fri Jun 19 23:12:10 CEST 2009


On 2009-06-19, Mitko Haralanov <mitko at qlogic.com> wrote:

> I have a question about finding out whether a string contains
> binary data?

All strings contain binary data.  Unless you've invented
ternary logic and built a computer with it. ;)

> If I read in a string containing some binary data from the file, the
> type of that string is <type 'str'> which is not different from any
> other string, so I can't use that as a check.

Correct.

> The only other check that I can think of is to check every
> character in the read-in string against string.printable but
> that will take a long time.

There's no such thing as a free lunch. 

If you want to know if some condition is true for every octet
in a string, then you have to check whether it's true for every
character in the string -- until you find it to be false.

IOW, you can stop when you find the first non-printable octect.

-- 
Grant Edwards                   grante             Yow! Eisenhower!!  Your
                                  at               mimeograph machine upsets
                               visi.com            my stomach!!



More information about the Python-list mailing list