title: Python: NULL bytes or control characters
body: <p>I'm currently working with lxml. I'm setting attributes, and I get the following error: </p>
  
  <blockquote>
    <p>ValueError: All strings must be XML compatible: Unicode or ASCII, no
    NULL bytes or control characters</p>
  </blockquote>
  
  <p>I used the same script for some years, and I haven't had any problem till now (I've got new files to process).</p>
  
  <p>How can I handle null bytes or control chars? The best solution probably is to ignore them. But how can this be done? </p>
  
  <p>I set new strings like this: </p>
  
  <pre><code>w.set("lem", newString)
  </code></pre>
  
  <p>With the help of Jack, I know that the following causes the error:</p>
  
  <blockquote>
    <p>"Bad string is: 'Bo\xdf'".</p>
  </blockquote>
  
  <p>How can I handle such cases? </p>
  
  <p>Edit:
  Using this function:</p>
  
  <pre><code>from curses import ascii
  def clean(text):
      return str(''.join(
              ascii.isprint(c) and c or '?' for c in text
              )) 
  </code></pre>
  
  <p>I don't get any error anymore. But the code sets a lot of "?". In place of the "?", I'd like to have the correct chars (in utf-8)... </p>