code: def istext(s, text_characters=None, threshold=0.3):
      
      text_characters = "".join(map(chr, range(32, 127))) + "\n\r\t\b"
      _null_trans = string.maketrans("", "")
      # if s contains any null, it's not text:
      if "\0" in s:
          return False
      # an "empty" string is "text" (arbitrary but reasonable choice):
      if not s:
          return True
      # Get the substring of s made up of non-text characters
      t = s.translate(_null_trans, text_characters)
      # s is 'text' if less than 30% of its characters are non-text ones:
      return len(t)/float(len(s)) <= threshold
docs: Determines if the string is a set of binary data or a text file.
      This is done by checking if a large proportion of characters are > 0X7E
      (0x7F is <DEL> and unprintable) or low bit control codes. In other words
      things that you wouldn't see (often) in a text file. (ASCII past 0x7F
      might appear, but rarely).
  
      Code modified from
      https://www.safaribooksonline.com/library/view/python-cookbook-2nd/0596007973/ch01s12.html