Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Splitting Strings within an Array

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.

like image 654
sbowde4 Avatar asked Mar 02 '26 22:03

sbowde4


1 Answers

You can remove the index part, by using substring:

for cmd in arr:
   exec(cmd[5:])
like image 89
Ankur Jyoti Phukan Avatar answered Mar 05 '26 11:03

Ankur Jyoti Phukan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!