On Tue, Jul 23, 2013 at 9:26 AM, Vincent Vande Vyvre < vincent.vandevy...@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
-- http://mail.python.org/mailman/listinfo/python-list