Working with strings in Python: Formatting the information in a very specific way
<p>OBS: I am working on Python 3.5.2 and I use the standard Shell and IDLE</p>

<p>Hi everyone, I think one of the most difficult tasks I have when programming with python is dealing with strings. Is there someone who can help?</p>

<p>One of the problems I face frequently is transforming the string so I can use it properly. For instance:</p>

<pre><code>#consider I have the following string I want to work with:
</code></pre>

<p><strong><code>my_input = 'insert 3 ["psyduck", 30]!!!insert 10 ["a nice day at the beach", 100]!!!find 3!!!find 10'</code></strong></p>

<p>How can I transform this string in a  way that, with the results, I would be able to do the following:</p>

<p><em>1 - Separate the following substrings into variables like this:</em></p>

<pre><code>command = 'insert'

node = '3' #or the int 3

list = '["psyduck", 30]'
</code></pre>

<p><em>2 - Or any other solution that will somehow enable me to do this in the end:</em></p>

<pre><code>listOfCommands = [['insert', '3', '["psyduck", 30]'], ['insert', '10', '["a nice day at the beach", 100]'], ['find', '3'], ['find', '10']]
</code></pre>

<p>I need this list in order to do the following:</p>

<pre><code>for entry in listOfCommands:
    if entry[0] == 'insert':
    #I will execute a part of the program    

    elif entry[0] == 'update':
    #execute something else

    elif entry[0] == 'find':
    #execute something else
</code></pre>

<p>The problem is that I do not know exaclty what is going to appear (the number of commands or the size of the information I will have to add) in the input. I just know that it will always obey these exact formats: 
<strong>[A command, a 'node' where I have to store the information or update it, the information I have to store or update]</strong> or <strong>[A command, a 'node' I have to find or delete]</strong> and the blocks will be separated by '!!!'</p>

<p>I can work my way around the main program but in order to be able to make it run properly, I need to have this input formatted in this really specific way.</p>

<p>Maybe something like this will work:</p>

<pre><code>commands = my_input.split('!!!')
my_commands = [c.split(' ', 2) for c in commands]
</code></pre>

<p>The second argument of the <code>split</code> methods tells it how many times you want it to split the string.</p>

python string python-3.x