re.match oddity

Hans Nowak wurmy at earthlink.net
Thu Dec 27 01:42:26 CET 2001


Issac wrote:
> 
> A curious thing is happening in my c2py.py script (below).
> The resulting files contain little ^M's (when viewed with the Windows
> version of gvim 6.0 but not when viewed with notepad.exe) at the ends
> of the lines.  When I remove the lines
> 
>     line_is_lone_left_curly = re.match(r'^\s*{\s*((//.*)|(/\*.*\*/))?$', line)
>     if line_is_lone_left_curly:
>       pylines[-1] = pylines[-1][:-1] + ':\n'
>       continue
> 
> the ^M's go away.  Does anyone know why?  I'm using Python 2.1.1
> on Cygwin with Windows 2000.

Not sure it has anything to do with re.match. Most likely it's caused 
by the line that appends ':\n'. This is probably written to file 
as '\r\n' (CR/LF) (in Windows). I suspect that some other lines
only have '\n' (LF) as a line separator, otherwise gvim would not show
them; it usually detects the line ending and displays the text
accordingly.

The solution is to make sure that your lines have either a Windows
or a Unix line ending, but not a mix. Use os.linesep when in doubt.

--Hans



More information about the Python-list mailing list