Converting text file to different encoding.

Marko Rauhamaa marko at pacujo.net
Fri Apr 17 16:51:59 CEST 2015


Chris Angelico <rosuav at gmail.com>:

> 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(source.read())

You might run out of memory. How about:

========================================================================
#!/usr/bin/env python3
import shutil
shutil.copyfileobj(
    open("source1", encoding="iso-8859-1"),
    open("target", "w", encoding="utf-8"))
========================================================================


Marko



More information about the Python-list mailing list