UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 46: ordinal not in range
<p>I know there're many UnicodeDecodeError around, but I can't find anyone explaining my issue.. And this error randomly jumped from an environment that I can't reproduce..</p>

<p>What I want is to just to compare two strings byte to byte (I don't want any encoding and decoding).</p>

<p>Note that I'm using python2.7 and str1 is from open('..', 'r').read() on linux.</p>

<p>Hope for your advices..</p>

<pre><code>  def diff_str(str1, str2):
      minlen = min(len(str1), len(str2))
      if str1 == str2:
        return "All %d bytes same" %minlen

      for diff_pos in xrange(minlen):
        if str1[diff_pos] != str2[diff_pos]:
          break

      k = 100

      to_ret = "(%d vs %d) chars\n" % (len(str1), len(str2))
      to_ret += "diff starts at %d:\n" % diff_pos
      # error jumps out at here..
      to_ret += str1[diff_pos:diff_pos+k] + "\n"
      to_ret += str2[diff_pos:diff_pos+k] + "\n"
    return to_ret
</code></pre>

<p>Firstly, please paste your string(or file) you want to compare and make the errors.</p>

<p>Have a try:</p>

<pre><code>for uchar in your_string.decode('utf-8'):
    # compare chars
</code></pre>

<p>Do you want a binary comparsion? Then give it a try:</p>

<pre><code>oneBuf = bytes(yourfile.read(1024))
</code></pre>

<p>Then compare the byte bufs.</p>

python