1

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.

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:

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

1 Answers1

1

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

if request.method == 'POST':
    ids = request.form['ids']
    f_n = request.form['f_n']
    IDlist = ids.split(',')

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

[['i'], ['d'], etc...

Hope that is helpful. This might be helpful - Split by comma and strip whitespace in Python

Community
  • 1
  • 1
A. N. Other
  • 392
  • 4
  • 14