Path: csiph.com!usenet.pasdenom.info!news.redatomik.org!newsfeed.xs4all.nl!newsfeed2a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'source,': 0.04; 'subject:text': 0.05; 'python3': 0.07; 'subject:file': 0.07; 'newline': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'python': 0.11; 'translation': 0.12; '2.7': 0.14; '"w")': 0.16; 'codecs': 0.16; 'preserved': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'subject:Converting': 0.16; 'sat,': 0.16; 'wrote:': 0.18; 'memory': 0.22; 'import': 0.22; 'header:User-Agent:1': 0.23; 'replace': 0.24; 'source': 0.25; 'header:X-Complaints-To:1': 0.27; 'tried': 0.27; 'chris': 0.29; 'am,': 0.29; '>>>>': 0.31; 'consumption': 0.31; 'disable': 0.31; 'open': 0.33; "i'd": 0.34; 'skip:s 30': 0.35; '2.6': 0.36; 'to:addr:python-list': 0.38; 'files': 0.38; 'sure': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'how': 0.40; 'skip:t 30': 0.61; 'limit': 0.70; '2015': 0.84; 'ok?': 0.84; 'viable': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: Converting text file to different encoding. Date: Fri, 17 Apr 2015 17:06:33 +0200 Organization: None References: <6bddf2c0-b0c3-456d-a699-d6c921e6fc73@googlegroups.com> <99b92c62-6fe0-4752-b64c-bff909faa6b8@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p57bd8f9c.dip0.t-ipconnect.de User-Agent: KNode/4.13.3 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 42 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1429283211 news.xs4all.nl 2840 [2001:888:2000:d::a6]:55858 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:89076 Chris Angelico wrote: > On Sat, Apr 18, 2015 at 12:26 AM, 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(source.read(), >>>>> 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(source.read()) This approach is also viable in Python 2.6 and 2.7 if you use io.open() instead of the builtin. To limit memory consumption for big files you can replace target.write(source.read()) with shutil.copyfileobj(source, target) If you want to be sure that line endings are preserved open both files with io.open(..., newline="") # disable newline translation