1

Is there a common way to parse arrays sent in this notation?

>>> import urlparse
>>> urlparse.parse_qs('a=1&a=2')['a']
['1', '2']

I'd expect the output of the following to be the same:

>>> urlparse.parse_qs('a[0]=1&a[1]=2')['a']
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'a'

Is there a reason why it isn't so?

2
  • Probably because that's a very unusual way of constructing a query string. Where is it coming from? Commented Sep 14, 2013 at 14:41
  • @DanielRoseman I'm seeing it while using mootools: var arr=[1,2]; new Request({url:'...'}).post({a: arr}); Commented Sep 14, 2013 at 15:48

1 Answer 1

3

Why would you expect the results from two entirely different query strings to be the same? You have two distinct keys in the your second query string: a[0] and a[1]. They are strings and urlparse does not further parse them. You will find them in the resulting dictionary under the names by which they appear in the query string.

If urlparse behaved as you seem to want it to, how would this be parsed?

a=1&a=2&a[0]=3&a[1]=4
Sign up to request clarification or add additional context in comments.

1 Comment

Semantically they are both array notations seen on the web. Your example I'd expect to be parsed as ['3', '4', '1', '2'].

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.