How to convert "\uxxxx\uxxxx" to u'\Uxxxxxxxx'?
<p>I have a text file who is filled with unicode characters as <code>"\ud83d\udca5"</code> but python don't seem to like them.<br>
But if I replace it by <code>u'\U0001f4a5'</code> which seems to be his python escape style (<a href="http://www.charbase.com/1f4a5-unicode-collision-symbol" rel="nofollow">Charbase</a>), it works.</p>

<p>Is there a solution to convert them all into the <code>u"\Uxxxxxxxx"</code> escape format than python can understand ?</p>

<p>Thanks.</p>

<p>You're mixing up Unicode and encoded strings. <code>u'\U0001f4a5'</code> is a Unicode object, Python's internal datatype for handling strings. (In Python 3, the <code>u</code> is optional since now <em>all</em> strings are Unicode objects).</p>

<p>Files, on the other hand, use encodings. UTF-8 is the most common one, but it's just one means of storing a Unicode object in a byte-oriented file or stream. When opening such a file, you need to specify the encoding so Python can translate the bytes into meaningful Unicode objects.</p>

<p>In your case, it seems you need to open file using the <code>UTF-16</code> codec instead of <code>UTF-8</code>. </p>

<pre><code>with open("myfile.txt", encoding="utf-16") as f:
    s = f.read()
</code></pre>

<p>will give you the proper contents if the codec is in fact <code>UTF-16</code>. If it doesn't look right, try <code>"utf-16-le"</code> or <code>"utf-16-be"</code>.</p>

python python-3.x unicode escaping unicode-escapes