title: In Python 3, how do you remove all non-UTF8 characters from a string?
body: <p>I'm using Python 3.7.  How do I remove all non-UTF-8 characters from a string?  I tried using "lambda x: x.decode('utf-8','ignore').encode("utf-8")" in the below</p>
  
  <pre><code>coop_types = map(
      lambda x: x.decode('utf-8','ignore').encode("utf-8"),
      filter(None, set(d['type'] for d in input_file))
  )
  </code></pre>
  
  <p>but this is resulting in the error ...</p>
  
  <pre><code>Traceback (most recent call last):
    File "scripts/parse_coop_csv.py", line 30, in &lt;module&gt;
      for coop_type in coop_types:
    File "scripts/parse_coop_csv.py", line 25, in &lt;lambda&gt;
      lambda x: x.decode('utf-8','ignore').encode("utf-8"),
  AttributeError: 'str' object has no attribute 'decode'
  </code></pre>
  
  <p>If you have a generic way to remove all non-UTF8 chars from a string, that's all I'm looking for.</p>