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


Groups > comp.lang.python > #89067

Re: Converting text file to different encoding.

Path csiph.com!usenet.pasdenom.info!news.redatomik.org!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <oscar.j.benjamin@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'encoding': 0.05; 'preferably': 0.05; 'string.': 0.05; 'subject:text': 0.05; 'skip:` 10': 0.07; 'subject:file': 0.07; 'string': 0.09; 'friday,': 0.09; '"w")': 0.16; 'buffer,': 0.16; 'codecs': 0.16; 'encoding.': 0.16; 'encodings,': 0.16; 'subject:Converting': 0.16; 'to:name:python list': 0.16; 'typeerror:': 0.16; 'wrote:': 0.18; 'trying': 0.19; '>>>': 0.22; 'import': 0.22; 'error': 0.23; 'source': 0.25; 'header:In-Reply-To:1': 0.27; 'message-id:@mail.gmail.com': 0.30; '"",': 0.31; 'file': 0.32; '(most': 0.33; 'skip:s 30': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'should': 0.36; 'to:addr :python-list': 0.38; 'files': 0.38; 'fact': 0.38; 'recent': 0.39; 'to:addr:python.org': 0.39; 'read': 0.60; 'email addr:gmail.com': 0.63; 'results': 0.69; 'default': 0.69; '2015': 0.84; 'oscar': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=TB129espaG6cLK7+dJ07NpvU1KfdaSjk/gdVzLLb/QA=; b=jrA6N3u9lcmEeKfOv2RH/htg4gcqGAGPoUId6fLsyeFb91MvoAJAJZESHCuxzbhm4F AVTx9fqMjD7uUEa/9pzlRzQrw7ORJknXfcFMGTILlTu7hLQ38xZlYHGz6ynhVVA2ZYLs VjCZFSDFchCL+Ow60ILr23uklMVhvMxTUd+tyA6x4/fKEq2wr2cGXSAwT77QOBONo1Ny sLbmerPh+s8ezaM5jn+sZg4H+R39AgLGDzz6+KhMBuJsDy3nm/+KqJY627vm+jsJyv+L yeK2HWckji1ulgPJrTxg1bKuP9MGcPDCVt0zD5caBYrnj6YZTp/XQMQSAz2bByooDiRK H+QA==
X-Received by 10.194.86.135 with SMTP id p7mr6424565wjz.89.1429279578109; Fri, 17 Apr 2015 07:06:18 -0700 (PDT)
MIME-Version 1.0
In-Reply-To <d921df37-c353-4e3b-8948-12ef05eb2870@googlegroups.com>
References <6bddf2c0-b0c3-456d-a699-d6c921e6fc73@googlegroups.com> <d921df37-c353-4e3b-8948-12ef05eb2870@googlegroups.com>
From Oscar Benjamin <oscar.j.benjamin@gmail.com>
Date Fri, 17 Apr 2015 15:05:52 +0100
Subject Re: Converting text file to different encoding.
To Python List <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.367.1429279585.12925.python-list@python.org> (permalink)
Lines 27
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1429279585 news.xs4all.nl 2909 [2001:888:2000:d::a6]:55989
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:89067

Show key headers only | View raw


On 17 April 2015 at 14:51,  <subhabrata.banerji@gmail.com> wrote:
> On Friday, April 17, 2015 at 6:50:08 PM UTC+5:30, subhabrat...@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")
>>    >>> 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

The error comes from `unicode(source, sourceEncoding)` and results
from the fact that source is a file object when it should be a string.
To read the contents of the file as a string just change `source` to
`source.read()`.


Oscar

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