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: 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: References: <6bddf2c0-b0c3-456d-a699-d6c921e6fc73@googlegroups.com> From: Oscar Benjamin Date: Fri, 17 Apr 2015 15:05:52 +0100 Subject: Re: Converting text file to different encoding. To: Python List 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 On 17 April 2015 at 14:51, 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 "", line 1, in >> 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