Python UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3
<p>I'm reading a config file in python getting sections and creating new config files for each section.</p>

<p>However.. I'm getting a decode error because one of the strings contains <code>EspaÃ±ol=spain</code></p>

<pre><code>self.output_file.write( what.replace( " = ", "=", 1 ) )
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 4: ordinal not in range(128)
</code></pre>

<p>How would I adjust my code to allow for encoded characters such as these? I'm very new to this so please excuse me if this is something simple..</p>

<pre><code>class EqualsSpaceRemover:
    output_file = None
    def __init__( self, new_output_file ):
        self.output_file = new_output_file

    def write( self, what ):
        self.output_file.write( what.replace( " = ", "=", 1 ) )

def get_sections():
    configFilePath = 'C:\\test.ini'
    config = ConfigParser.ConfigParser()
    config.optionxform = str
    config.read(configFilePath)
    for section in config.sections():
        configdata = {k:v for k,v in config.items(section)}
        confignew = ConfigParser.ConfigParser()
        cfgfile = open("C:\\" + section + ".ini", 'w')
        confignew.add_section(section)
        for x in configdata.items():
            confignew.set(section,x[0],x[1])
        confignew.write( EqualsSpaceRemover( cfgfile ) )
        cfgfile.close()
</code></pre>

<p>If you use <code>python2</code> with <code>from __future__ import unicode_literals</code> then every string literal you write is an unicode literal, as if you would prefix every literal with <code>u"..."</code>, unless you explicityl write <code>b"..."</code>.</p>

<p>This explains why you get an Unicode<i>Decode</i>Error on this line:</p>

<pre><code>what.replace(" = ", "=", 1)
</code></pre>

<p>because what you actually do is</p>

<pre><code>what.replace(u" = ",u"=",1 )
</code></pre>

<p><code>ConfigParser</code> uses plain old <code>str</code> for its items when it reads a file using the <code>parser.read()</code> method, which means <code>what</code> will be a <code>str</code>. If you use unicode as arguments to <code>str.replace()</code>, then the string is converted (decoded) to unicode, the replacement applied and the result returned as unicode. But if <code>what</code> contains characters that can't be decoded to unicode using the default encoding, then you get an UnicodeDecodeError where you wouldn't expect one.</p>

<p>So to make this work you can</p>

<ul>
<li>use explicit prefixes for byte strings: <code>what.replace(b" = ", b"=", 1)</code></li>
<li>or remove the <code>unicode_litreals</code> future import.</li>
</ul>

<p>Generally you shouldn't mix <code>unicode</code> and <code>str</code> (python3 fixes this by making it an error in almost any case). You should be aware that <code>from __future__ import unicode_literals</code> changes every non prefixed literal to unicode and doesn't automatically change your code to work with unicode in all case. Quite the opposite in many cases.</p>

python parsing configparser python-config