Python Flask Split Text in Form by comma
<p>I'm writing a function in Flask that takes a list of IDs and sends a request to Amazon for data on those IDs. I'm running into an issue where the contents of the form are not splitting the way I want it to. I'd like it to be split by a comma or whitespace so that each ID is sent to the request, but instead it's sending a request for each individual character in the form. </p>

<p>As an example, I would put IDs in the form as 'id-12345, id-24689' and it would send a request for those ids. Rather, it is sending a request for 'i', 'd',' '-', etc... Here's the code for the form request:</p>

<pre><code>if request.method == 'POST':
    IDlist = []
    ids = request.form['ids']
    f_n = request.form['f_n']
    for i in ids:
        i.split(',')
        IDlist.append(i)
</code></pre>

<p>You have a for loop that you don't need. Instead the following would work</p>

<pre><code>if request.method == 'POST':
    ids = request.form['ids']
    f_n = request.form['f_n']
    IDlist = ids.split(',')
</code></pre>

<p>What you are doing is you are looping through each individual character, splitting each character e.g. "i" by a comma (which returns the item ["i"]), and then appending that to the list. What you are therefore getting is the following list</p>

<pre><code>[['i'], ['d'], etc...
</code></pre>

<p>Hope that is helpful. This might be helpful - <a href="http://stackoverflow.com/questions/4071396/split-by-comma-and-strip-whitespace-in-python?rq=1">Split by comma and strip whitespace in Python</a></p>

python forms flask