How to stop Python parse_qs from parsing single values into lists?

前端 未结 2 686
予麋鹿
予麋鹿 2020-12-28 12:15

In python 2.6, the following code:

import urlparse
qsdata = \"test=test&test2=test2&test2=test3\"
qs = urlparse.parse_qs(qsdata)
print qs
         


        
2条回答
  •  鱼传尺愫
    2020-12-28 12:34

    You could fix it afterwards...

    import urlparse
    qsdata = "test=test&test2=test2&test2=test3"
    qs = dict( (k, v if len(v)>1 else v[0] ) 
               for k, v in urlparse.parse_qs(qsdata).iteritems() )
    print qs
    

    However, I don't think I would want this. If a parameter that is normally a list happens to arrive with only one item set, then I would have a string instead of the list of strings I normally receive.

提交回复
热议问题