Converting text file to different encoding.

Chris Angelico rosuav at
Fri Apr 17 16:41:56 CEST 2015

On Sat, Apr 18, 2015 at 12:26 AM,  <subhabrata.banerji at> wrote:
> I tried to do as follows,
>>>> import codecs
>>>> sourceEncoding = "iso-8859-1"
>>>> targetEncoding = "utf-8"
>>>> source = open("source1","w")
>>>> string1="String type"
>>>> str1=str(string1)
>>>> source.write(str1)
>>>> source.close()
>>>> target = open("target", "w")
>>>> source=open("source1","r")
>>>> target.write(unicode(, sourceEncoding).encode(targetEncoding))
> am I going ok?

Here's how I'd do it.

$ python3
>>> with open("source1", encoding="iso-8859-1") as source, open("target", "w", encoding="utf-8") as target:
...     target.write(

Or maybe this:

$ pike
> Stdio.write_file("target", string_to_utf8(Stdio.read_file("source1")));

So much easier than fiddling around with all those steps you're doing.
I'm not sure what they're all for, anyway; calling str() on a
double-quoted literal isn't usually going to do anything, and I don't
see "from __future__ import unicode_literals" anywhere.


More information about the Python-list mailing list