Strange behaviour with os.linesep
Jason Swails
jason.swails at gmail.com
Tue Jul 23 09:35:27 EDT 2013
On Tue, Jul 23, 2013 at 9:26 AM, Vincent Vande Vyvre <
vincent.vandevyvre at swing.be> wrote:
> Le 23/07/2013 15:10, Vincent Vande Vyvre a écrit :
>
> The '\n' are in the original file.
>>
>> I've tested these other versions:
>>
>> ------------------------------**-
>> def write():
>> strings = ['# -*- coding: utf-8 -*-\n',
>> 'import os\n',
>> 'import sys\n']
>> with open('writetest.py', 'w') as outf:
>> txt = L_SEP.join([s.rstip() for s in strings]):
>> outf.write(txt)
>> ------------------------------
>>
>> ------------------------------**-
>> def write():
>> strings = ['# -*- coding: utf-8 -*-',
>> 'import os',
>> 'import sys']
>> with open('writetest.py', 'w') as outf:
>> txt = L_SEP.join( strings):
>> outf.write(txt)
>> ------------------------------
>>
>> Las, no changes, always correctly displayed in MS bloc-notes but with
>> double line in other éditors.
>>
>>
> Also with:
>
> ------------------------------**----------
> def count():
> with open('c:\\Users\\Vincent\\**writetest.py', 'r') as inf:
> lines = 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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20130723/3212104d/attachment.html>
More information about the Python-list
mailing list