title: Decode individual octal characters in string variable
body: <p>A string variable sometimes includes octal characters that need to be un-octaled. Example: <code>oct_var = &quot;String\302\240with\302\240octals&quot;</code>, the value of <code>oct_var</code> should be <code>&quot;String with octals&quot;</code> with non-breaking spaces.</p>
  <p><a href="https://docs.python.org/3.9/library/codecs.html" rel="nofollow noreferrer">Codecs</a> doesn't support octal, and I failed to find a working solution with <code>encode()</code>. The strings originate upstream outside my control.</p>
  <p>Python 3.9.8</p>
  <p>Edited to add:
  It doesn't have to scale or be ultra fast, so maybe the idea from <a href="https://bytes.com/topic/python/answers/743965-converting-octal-escaped-utf-8-a" rel="nofollow noreferrer">here</a> (#6) can work (not tested yet):</p>
  <pre><code>def decode(encoded):
      for octc in (c for c in re.findall(r'\\(\d{3})', encoded)):
          encoded = encoded.replace(r'\%s' % octc, chr(int(octc, 8)))
      return encoded.decode('utf8')
  </code></pre>