Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #89071

Re: Converting text file to different encoding.

Path csiph.com!usenet.pasdenom.info!nntpfeed.proxad.net!proxad.net!feeder1-1.proxad.net!ecngs!feeder2.ecngs.de!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed1a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <rosuav@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.002
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'source,': 0.04; 'subject:text': 0.05; 'python3': 0.07; 'subject:file': 0.07; 'literal': 0.09; 'cc:addr:python-list': 0.11; '"from': 0.16; '"w")': 0.16; '__future__': 0.16; 'codecs': 0.16; 'for,': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'str()': 0.16; 'subject:Converting': 0.16; 'sat,': 0.16; 'wrote:': 0.18; '>>>': 0.22; 'import': 0.22; 'cc:addr:python.org': 0.22; 'cc:2**0': 0.24; 'source': 0.25; 'this:': 0.26; 'header:In-Reply- To:1': 0.27; 'tried': 0.27; 'am,': 0.29; 'message- id:@mail.gmail.com': 0.30; "i'm": 0.30; 'easier': 0.31; 'usually': 0.31; '>>>>': 0.31; 'maybe': 0.34; "i'd": 0.34; 'skip:s 30': 0.35; 'received:google.com': 0.35; 'sure': 0.39; 'how': 0.40; 'skip:u 10': 0.60; 'skip:t 30': 0.61; "you're": 0.61; '2015': 0.84; 'ok?': 0.84; 'pike': 0.84; 'to:none': 0.92
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=CpTbfGBwTFPCNAolBO4GUUIDR/zFZkk6Chvrlmf6uW8=; b=HGlkGf92g0beMCUuIy2Aqr8a8dJJcLqUX7Eu/ZaHj+dxD+clyEGziW2zcIppTbpIju bUmoWlGYWv852eEwF6W3EIlagbxNv+6DIHWetmd7Uzh/t1IUtz6IKP1B4QoJFY/duU+h 4Lm3cqsS0ti070JvnlFnZHfUoTrPvoa6qwoLpBnyLo13X7F4J3L0xhdwdcWZi7SY1YlC lV1mKM/BTppZUp3qtQXHA0eqU7xjlSU4o9K4ueSxWont43vabFGZzenc4xjnBuxk/imz JWpb0L6LfASpFDPaezPycq4T5NET4/pHOjplpaFPiAx4Mt1qaFqnXbUE4oD/OMaxhiRl Dnmw==
MIME-Version 1.0
X-Received by 10.50.176.137 with SMTP id ci9mr3399076igc.2.1429281716597; Fri, 17 Apr 2015 07:41:56 -0700 (PDT)
In-Reply-To <99b92c62-6fe0-4752-b64c-bff909faa6b8@googlegroups.com>
References <6bddf2c0-b0c3-456d-a699-d6c921e6fc73@googlegroups.com> <d921df37-c353-4e3b-8948-12ef05eb2870@googlegroups.com> <mailman.367.1429279585.12925.python-list@python.org> <99b92c62-6fe0-4752-b64c-bff909faa6b8@googlegroups.com>
Date Sat, 18 Apr 2015 00:41:56 +1000
Subject Re: Converting text file to different encoding.
From Chris Angelico <rosuav@gmail.com>
Cc "python-list@python.org" <python-list@python.org>
Content-Type text/plain; charset=UTF-8
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.370.1429281718.12925.python-list@python.org> (permalink)
Lines 34
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1429281718 news.xs4all.nl 2841 [2001:888:2000:d::a6]:41181
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:89071

Show key headers only | View raw


On Sat, Apr 18, 2015 at 12:26 AM,  <subhabrata.banerji@gmail.com> 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())

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.

ChrisA

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Converting text file to different encoding. subhabrata.banerji@gmail.com - 2015-04-17 06:19 -0700
  Re: Converting text file to different encoding. Rustom Mody <rustompmody@gmail.com> - 2015-04-17 06:31 -0700
  Re: Converting text file to different encoding. subhabrata.banerji@gmail.com - 2015-04-17 06:51 -0700
    Re: Converting text file to different encoding. Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2015-04-17 15:05 +0100
      Re: Converting text file to different encoding. subhabrata.banerji@gmail.com - 2015-04-17 07:26 -0700
        Re: Converting text file to different encoding. Chris Angelico <rosuav@gmail.com> - 2015-04-18 00:41 +1000
          Re: Converting text file to different encoding. Marko Rauhamaa <marko@pacujo.net> - 2015-04-17 17:51 +0300
        Re: Converting text file to different encoding. Peter Otten <__peter__@web.de> - 2015-04-17 17:06 +0200
  Re: Converting text file to different encoding. Dave Angel <davea@davea.name> - 2015-04-17 10:48 -0400
  Re: Converting text file to different encoding. Dave Angel <d@davea.name> - 2015-04-17 10:57 -0400

csiph-web