[英]How can i convert the dictionary items into flat strings in python
我有項目字典,我從中生成這樣的URL
request.build_absolute_uri("/myurl/" + urlencode(myparams))
我得到的輸出是這樣的
number=['543543']®ion=['5,36,37']
但我想要的網址是
number=543543®ion=5,36,37
所有這些項目都在myparams dictionary
您可能會發現,為了便於使用,傳遞doseq=True
將是有用的(盡管不完全是您想要的 - 但確實意味着任何url解析庫都應該能夠處理輸入而無需自定義編碼......)
>>> from urllib import urlencode
>>> a = range(3)
>>> urlencode({'test': a})
'test=%5B0%2C+1%2C+2%5D'
>>> urlencode({'test': a}, True)
'test=0&test=1&test=2'
否則,您必須將自定義代碼寫入','.join(str(el) for el in your_list)
for myparams
中的值,它是一個列表/類似...(然后.split(',')
另一個結束)
看起來myparams
是一個將list
作為值的dict
。
new_params = dict(k, v[0] for k, v in dict.iteritems())
將構建一個新的詞典。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.