Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!goblin2!goblin.stu.neva.ru!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!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; '-*-': 0.07; 'nicely': 0.07; 'utf-8': 0.07; 'coding:': 0.09; 'comment,': 0.09; 'iterate': 0.09; 'lines:': 0.09; 'cc:addr:python-list': 0.11; 'def': 0.12; "'w')": 0.16; '23,': 0.16; 'cc:name:python list': 0.16; 'line).': 0.16; 'luck,': 0.16; '\xe9crit': 0.16; 'wrote:': 0.18; 'code.': 0.18; 'seems': 0.21; 'machine': 0.22; 'cc:addr:python.org': 0.22; 'file.': 0.24; 'cc:2**0': 0.24; "i've": 0.25; 'changes,': 0.26; 'header:In-Reply-To:1': 0.27; 'skip:- 40': 0.29; 'am,': 0.29; 'message-id:@mail.gmail.com': 0.30; 'lines': 0.31; 'txt': 0.31; 'file': 0.32; 'skip:- 30': 0.32; 'text': 0.33; 'linux': 0.33; 'subject:with': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'list': 0.37; 'skip:o 20': 0.38; 'displays': 0.38; 'jason': 0.38; 'issue': 0.38; 'rather': 0.38; 'more': 0.64; '(that': 0.65; 'jul': 0.74; '2013': 0.98 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:to :cc:content-type; bh=zGFJgVvL2xDsSE/FdDaz+o4d4lQqCD/EMH1xaVdgzHQ=; b=kQgW5m9Kk7pTgwXPndQvKwDtBe3gCxdfCJTGkrKKwzE9ljAjgzZAcn8RVLww+xBkO9 60/EkxZkKcTEfeMhSRYC38xJanILHWgZd5vHmUJscJbXNB7zbWCXS7qdmPXsheLb7t/k mEpHPjFs3NHTh3tnUsUO8ppUkLO5EYYygrG5B/4sR+zvcwaSiS8hIisJAUYMpwVltTQ5 2XecECJBQJcqM+If4QVxda9qsZlnqiP5ge6QjBHtGjoQqIUAvlyuH9rNiUG12RHL3Pxn t8s2Hsebmge0Dm72vgGFNOMXKCUkaKSSHufv604iDOJlID5F2F/X8mYIc7Xrtd24DDKx 0SrA== MIME-Version: 1.0 X-Received: by 10.42.36.198 with SMTP id v6mr18817293icd.58.1374586527595; Tue, 23 Jul 2013 06:35:27 -0700 (PDT) In-Reply-To: <51EE846F.7010209@swing.be> References: <368qu85msgfhuk2j2s13qj0bqn4rkcint9@4ax.com> <51ED3CEB.1070706@gmail.com> <51EE6C15.5070701@swing.be> <51EE80CB.2010100@swing.be> <51EE846F.7010209@swing.be> Date: Tue, 23 Jul 2013 09:35:27 -0400 Subject: Re: Strange behaviour with os.linesep From: Jason Swails To: Vincent Vande Vyvre Content-Type: multipart/alternative; boundary=001a11c3a2b0b0c1b004e22dddf9 Cc: python list X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 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: 152 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1374586530 news.xs4all.nl 15996 [2001:888:2000:d::a6]:42001 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:51089 --001a11c3a2b0b0c1b004e22dddf9 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable On Tue, Jul 23, 2013 at 9:26 AM, Vincent Vande Vyvre < vincent.vandevyvre@swing.be> wrote: > Le 23/07/2013 15:10, Vincent Vande Vyvre a =E9crit : > > The '\n' are in the original file. >> >> I've tested these other versions: >> >> ------------------------------**- >> def write(): >> strings =3D ['# -*- coding: utf-8 -*-\n', >> 'import os\n', >> 'import sys\n'] >> with open('writetest.py', 'w') as outf: >> txt =3D L_SEP.join([s.rstip() for s in strings]): >> outf.write(txt) >> ------------------------------ >> >> ------------------------------**- >> def write(): >> strings =3D ['# -*- coding: utf-8 -*-', >> 'import os', >> 'import sys'] >> with open('writetest.py', 'w') as outf: >> txt =3D L_SEP.join( strings): >> outf.write(txt) >> ------------------------------ >> >> Las, no changes, always correctly displayed in MS bloc-notes but with >> double line in other =E9ditors. >> >> > Also with: > > ------------------------------**---------- > def count(): > with open('c:\\Users\\Vincent\\**writetest.py', 'r') as inf: > lines =3D inf.readlines() > for l in lines: > print(l, len(l)) > Unrelated comment, but in general it's (much) more efficient to iterate through a file rather than iterate through a list of strings generated by readlines(): def count(): with open('c:\\Users\\Vincent\\writetest.py', 'r') as inf: for l in lines: print(l, len(l)) It's also fewer lines of code. ('# -*- coding: utf-8 -*-\r\n', 25) > ('import os\r\n', 11) > ('import sys', 10) Then it seems like there is an issue with your text editors that do not play nicely with DOS-style line endings. Gedit on my linux machine displays the line endings correctly (that is, '\r\n' is rendered as a single line). Good luck, Jason --001a11c3a2b0b0c1b004e22dddf9 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable

<= /div>


On Tue, = Jul 23, 2013 at 9:26 AM, Vincent Vande Vyvre <vincent.vandevyvr= e@swing.be> wrote:
Le 23/07/2013 15:10, Vincent Vande Vyvre a = =E9crit :

The '\n' are in the original file.

I've tested these other versions:

-------------------------------
def write():
=A0 =A0 strings =3D ['# -*- coding: utf-8 -*-\n',
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 'import os\n',
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 'import sys\n']
=A0 =A0 with open('writetest.py', 'w') as outf:
=A0 =A0 =A0 =A0 txt =3D L_SEP.join([s.rstip() for s in strings]):
=A0 =A0 =A0 =A0 outf.write(txt)
------------------------------

-------------------------------
def write():
=A0 =A0 strings =3D ['# -*- coding: utf-8 -*-',
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 'import os',
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 'import sys']
=A0 =A0 with open('writetest.py', 'w') as outf:
=A0 =A0 =A0 =A0 txt =3D L_SEP.join( strings):
=A0 =A0 =A0 =A0 outf.write(txt)
------------------------------

Las, no changes, always correctly displayed in MS bloc-notes but with doubl= e line in other =E9ditors.


Also with:

----------------------------------------
def count():
=A0 =A0 with open('c:\\Users\\Vincent\\writetest.py', 'r= ') as inf:
=A0 =A0 =A0 =A0 lines =3D inf.readlines()
=A0 =A0 for l in lines:
=A0 =A0 =A0 =A0 print(l, len(l))

Unrelated comment, but in gen= eral it's (much) more efficient to iterate through a file rather than i= terate through a list of strings generated by readlines():

def count():
=A0 =A0 with open('c:\\Us= ers\\Vincent\\writetest.py', 'r') as inf:
=A0 =A0 =A0 =A0 for= l in lines:
= =A0 =A0 =A0 =A0 =A0 =A0 print(l, len(l))

It's also= fewer lines of code.

('# -*- coding: utf-8 -*-\r\n', 25)
('import os\r\n', 11)
('import sys', 10)

Then it seems like there is an issue wit= h your text editors that do not play nicely with DOS-style line endings. = =A0Gedit on my linux machine displays the line endings correctly (that is, = '\r\n' is rendered as a single line).

Good luck,
Jason
--001a11c3a2b0b0c1b004e22dddf9--