Converting text file to different encoding.

Rustom Mody rustompmody at gmail.com
Fri Apr 17 09:31:34 EDT 2015


On Friday, April 17, 2015 at 6:50:08 PM UTC+5:30, subhabrat... at gmail.com wrote:
> I am having few files in default encoding. I wanted to change their encodings, 
> preferably in "UTF-8", or may be from one encoding to any other encoding. 
> 
> I was trying it as follows,
> 
>    >>> import codecs
>    >>> sourceEncoding = "iso-8859-1"
>    >>> targetEncoding = "utf-8"
>    >>> source = open("source1","w")

Do you want "w" or "r" ?

>    >>> target = open("target", "w")
>    >>> target.write(unicode(source, sourceEncoding).encode(targetEncoding))
> 
> but it was giving me error as follows,
> Traceback (most recent call last):
>   File "<pyshell#6>", line 1, in <module>
>     target.write(unicode(source, sourceEncoding).encode(targetEncoding))
> TypeError: coercing to Unicode: need string or buffer, file found
> 
> If anybody may kindly suggest how may I solve it. 
> 
> Regards,
> Subhabrata Banerjee.




More information about the Python-list mailing list