Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #89076
| 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 | <python-python-list@m.gmane.org> |
| 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> <d921df37-c353-4e3b-8948-12ef05eb2870@googlegroups.com> <mailman.367.1429279585.12925.python-list@python.org> <99b92c62-6fe0-4752-b64c-bff909faa6b8@googlegroups.com> <CAPTjJmo3o0rBrP2-51DbtcDYZy-Yo5PxLXpVVFc0R0SmL2nRsA@mail.gmail.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 <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.373.1429283211.12925.python-list@python.org> (permalink) |
| 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 |
Show key headers only | View raw
Chris Angelico wrote:
> 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())
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
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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