3

I am writing a program in python that reads in a text file and executes any python commands within it. The commands may be out of order, but each command has a letter ID such as {% (c) print x %}

I've been able to sort all the commands with in the document into an array, in the correct order. My question is, how to i remove the (c), so i can run exec(statement) on the string?

Here is the full example array

[' (a) import random ', ' (b) x = random.randint(1,6) ', ' (c) print x ', ' (d) print 2*x ']

Also, I am very new to python, my first assignment with it.

JJJ
  • 32,902
  • 20
  • 89
  • 102
sbowde4
  • 767
  • 1
  • 4
  • 23

5 Answers5

1

Take everything right to the parenthesis and exec:

for cmd in arr:
    exec(cmd.split(") ")[-1])
Marat
  • 15,215
  • 2
  • 39
  • 48
  • I see how this works, and I like it. How ever I get an invalid syntax error for my for loop. I used my own variables of course, but once I figure that error out I will probably use this. – sbowde4 Mar 15 '17 at 03:48
1

You can remove the index part, by using substring:

for cmd in arr:
   exec(cmd[5:])
Ankur Jyoti Phukan
  • 785
  • 3
  • 12
  • 20
  • 1
    This was the quickest method, and it worked the best because I didn't want to completely remove the index. On account of it will be used later. – sbowde4 Mar 15 '17 at 03:59
  • It is nice that it is short, quick and easily explained :-) However, a regex approach will survive changes to the whitespace and having a command-id of more than one character for a bigger input. – Raymond Hettinger Mar 15 '17 at 04:10
1

Stripping the command-id prefixes is a good job for a regular expression:

>>> import re
>>> commands = [' (a) import random ', ' (b) x = random.randint(1,6) ', ' (c) print x ', ' (d) print 2*x ']
>>> [re.search(r'.*?\)\s*(.*)', command).group(1) for command in commands]
['import random ', 'x = random.randint(1,6) ', 'print x ', 'print 2*x ']

The meaning of regex components are:

  • .*?\) means "Get the shortest group of any characters that ends with a closing-parentheses."

  • \s* means "Zero or more space characters."

  • (.*) means "Collect all the remaining characters into group(1)."

How this explanation makes it all clear :-)

Raymond Hettinger
  • 216,523
  • 63
  • 388
  • 485
0

Since the pattern looks simple and consistent, you could use regex.

This also allows for both (a) and (abc123) as valid IDs.

import re

lines = [
    ' (a) import random ',
    ' (b) x = random.randint(1,6) ',
    ' (c) print x ',
    ' (d) print 2*x '
]

for line in lines:
    print(re.sub(r"^[ \t]+(\(\w+\))", "", line))

Which would output:

 import random 
 x = random.randint(1,6) 
 print x 
 print 2*x 

If you really only want to match a single letter, then replace \w+ with [a-zA-Z].

vallentin
  • 23,478
  • 6
  • 59
  • 81
0

You may use a simple regex to omit the first alpha character in braces as:

import re

lst = [' (a) import random ', ' (b) x = random.randint(1,6) ', ' (c) print x ', ' (d) print 2*x ']

for ele in lst:
    print re.sub("^ \([a-z]\)", "", ele)
ZdaR
  • 22,343
  • 7
  • 66
  • 87