Generating a python file

Posted by fema on Stack Overflow See other posts from Stack Overflow or by fema
Published on 2010-05-31T17:22:38Z Indexed on 2010/05/31 17:33 UTC
Read the original article Hit count: 183

Hi!

I'm havin issues with python (imo python itself is an issue).

I have a txt file, it contains a custom language and I have to translate it to a working python code.

The input:

import sys  

n = int(sys.argv[1]) ;;print "Beginning of the program!"

LOOP i in range(1,n) {print "The number:";;print i}

BRANCH n < 5 {print n ;;print "less than 5"}  

The wanted output looks exactly like this:

import sys  

n = int(sys.argv[1])   
print "Beginning of the program!"

for i in range(1,n) :  
    print "The number:"  
    print i  

if n < 5 :  
    print n   
    print "less than 5"    

The name of the input file is read from parameter. The out file is out.py. In case of a wrong parameter, it gives an error message. The ;; means a new line.

When I tried to do it, I made an array, I read all the lines into it, split by " ". Then I wanted to strip it from the marks I don't need. I made 2 loops, one for the lines, one for the words. So then I started to replace the things. Everything went fine until it came to the } mark. It finds it, but it can not replace or strip it. I have no more idea what to do.

Could someone help me, please? Thanks in advance!

© Stack Overflow or respective owner

Related posts about python

Related posts about compiler