Jaideep Samuel
Jaideep Samuel

Reputation: 11

Form Data to get a particular output from urlencode

What should be the dictionary form_data

Desired Output from python code >> data = parse.urlencode(form_data).encode():

"entry.330812148_sentinel=&entry.330812148=Test1&entry.330812148=Test2&entry.330812148=Test3&entry.330812148=Test4"

I tried various dictionary structures including ones with None, [] and dictionary within dictionary but I am unable to get this output

form_data = {'entry.330812148_sentinel':None, 'entry.330812148':'Test1', 'entry.330812148':'Test2', 'entry.330812148':'Test3', 'entry.330812148':'Test4'}

from urllib import request, parse data = parse.urlencode(form_data).encode() print("Printing Parsed Form Data........")

"entry.330812148_sentinel=&entry.330812148=Test1&entry.330812148=Test2&entry.330812148=Test3&entry.330812148=Test4"

Upvotes: 0

Views: 38

Answers (1)

isandeep
isandeep

Reputation: 46

You can use parse_qs from urllib.parse to return the python data structure

import urllib.parse
>>> s = 'entry.330812148_sentinel=&entry.330812148=Test1&entry.330812148=Test2&entry.330812148=Test3&entry.330812148=Test4'

>>> d1 = urllib.parse.parse_qs(s)
>>> d1
   {b'entry.330812148': [b'Test1', b'Test2', b'Test3', b'Test4']}

Upvotes: 0

Related Questions