re.match oddity

Issac issac at myfirstlink.net
Wed Dec 26 23:03:25 CET 2001


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.

Issac

=== %< ===

#!/usr/bin/env python
# c2py.py : partial conversion from C to Python
import re, sys

def c2py(pyfile, cfile):
  clines = cfile.readlines()
  pylines = []
  for line in clines:
    # semicolons
    line = re.sub(r';', '', line)

    # curly braces
    line_is_lone_left_curly = re.match(r'^\s*{\s*((//.*)|(/\*.*\*/))?$', line)
    if line_is_lone_left_curly:
      pylines[-1] = pylines[-1][:-1] + ':\n'
      continue
    line = re.sub(r'{', ':', line)
    line = re.sub(r'}', '', line)

    # comments
    line = re.sub(r'//', '#', line)
    
    line_is_blank = re.match(r'^\s*$', line)
    if not line_is_blank:
      pylines.append(line)
    
  for line in pylines:
    pyfile.write(line)

for cname in sys.argv[1:]:
  pyname = re.sub(r'\..*$', '.py', cname)
  print 'generating '+pyname+' from '+cname
  cfile = open(cname, 'r')
  pyfile = open(pyname, 'w')
  c2py(pyfile, cfile)
  cfile.close()
  pyfile.close()



---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.310 / Virus Database: 171 - Release Date: 12/19/2001





More information about the Python-list mailing list