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.

7
  • sample of array , so that we can find a way to remove Commented Mar 15, 2017 at 3:35
  • Split, take all but the first, second, and last, then join with spaces Commented Mar 15, 2017 at 3:35
  • str.replace('(c)', '') Commented Mar 15, 2017 at 3:37
  • @AnkurJyotiPhukan meant to add that. editing now Commented Mar 15, 2017 at 3:37
  • 1
    Possible duplicate of How to delete a character from a string using python? Commented Mar 15, 2017 at 3:38

5 Answers 5

1

You can remove the index part, by using substring:

for cmd in arr:
   exec(cmd[5:])
Sign up to request clarification or add additional context in comments.

2 Comments

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.
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.
1

Take everything right to the parenthesis and exec:

for cmd in arr:
    exec(cmd.split(") ")[-1])

1 Comment

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.
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 :-)

Comments

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].

Comments

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.