2017-03-17 190 views
0

我想字符串轉換的字典和我的字符串是遵循字符串字典轉換

order_id=BW_225996&tracking_id=306003083135&bank_ref_no=1489730168508&order_status=Success&failure_message=&payment_mode=Net Banking&card_name=AvenuesTest&status_code=null&status_message=Y&currency=INR&amount=100.0&billing_name=test&billing_address=test&billing_city=Pune&billing_state=Maharashtra&billing_zip=411041&billing_country=India&billing_tel=1234567890&[email protected]&delivery_name=test&delivery_address=test&delivery_city=Pune&delivery_state=Maharashtra&delivery_zip=411041&delivery_country=India&delivery_tel=1234567890&merchant_param1=3&merchant_param2=&merchant_param3=&merchant_param4=&merchant_param5=&vault=N&offer_type=null&offer_code=null&discount_value=0.0&mer_amount=100.0&eci_value=null&retry=N&response_code=0&billing_notes=&trans_date=17/03/2017 11:27:30&bin_country= 
+0

這是查詢字符串。 Django已經內置了對這些解析的支持。 –

回答

1

您可以使用split方法拆分此字符串:

c="order_id=BW_225996&tracking_id=306003083135" 

print {p.split("=")[0]:p.split("=")[1] for p in c.split("&")} 

輸出:

{'order_id': 'BW_225996', 'tracking_id': '306003083135'} 
+0

非常感謝你的回覆我的問題已經解決 –

0
_str="order_id=BW_225996&tracking_id=306003083135&bank_ref_no=1489730168508&order_status=Success&failure_message=&payment_mode=Net Banking&card_name=AvenuesTest&status_code=null&status_message=Y&currency=INR&amount=100.0&billing_name=test&billing_address=test&billing_city=Pune&billing_state=Maharashtra&billing_zip=411041&billing_country=India&billing_tel=1234567890&[email protected]&delivery_name=test&delivery_address=test&delivery_city=Pune&delivery_state=Maharashtra&delivery_zip=411041&delivery_country=India&delivery_tel=1234567890&merchant_param1=3&merchant_param2=&merchant_param3=&merchant_param4=&merchant_param5=&vault=N&offer_type=null&offer_code=null&discount_value=0.0&mer_amount=100.0&eci_value=null&retry=N&response_code=0&billing_notes=&trans_date=17/03/2017 11:27:30&bin_country=" 

{a:b for a,b in [i.split("=") for i in _str.split('&')]} 
+0

非常感謝你的回覆 –

0

您可以嘗試:

>>> s = """order_id=BW_225996&tracking_id=306003083135&bank_ref_no=1489730168508&order_status=Success&failure_message=&payment_mode=Net Banking&card_name=AvenuesTest&status_code=null&status_message=Y&currency=INR&amount=100.0&billing_name=test&billing_address=test&billing_city=Pune&billing_state=Maharashtra&billing_zip=411041&billing_country=India&billing_tel=1234567890&[email protected]&delivery_name=test&delivery_address=test&delivery_city=Pune&delivery_state=Maharashtra&delivery_zip=411041&delivery_country=India&delivery_tel=1234567890&merchant_param1=3&merchant_param2=&merchant_param3=&merchant_param4=&merchant_param5=&vault=N&offer_type=null&offer_code=null&discount_value=0.0&mer_amount=100.0&eci_value=null&retry=N&response_code=0&billing_notes=&trans_date=17/03/2017 11:27:30""" 
>>> s_list = s.split("&") 
>>> s_list 
['order_id=BW_225996', 'tracking_id=306003083135', 'bank_ref_no=1489730168508', 'order_status=Success', 'failure_message=', 'payment_mode=Net Banking', 'card_name=AvenuesTest', 'status_code=null', 'status_message=Y', 'currency=INR', 'amount=100.0', 'billing_name=test', 'billing_address=test', 'billing_city=Pune', 'billing_state=Maharashtra', 'billing_zip=411041', 'billing_country=India', 'billing_tel=1234567890', '[email protected]', 'delivery_name=test', 'delivery_address=test', 'delivery_city=Pune', 'delivery_state=Maharashtra', 'delivery_zip=411041', 'delivery_country=India', 'delivery_tel=1234567890', 'merchant_param1=3', 'merchant_param2=', 'merchant_param3=', 'merchant_param4=', 'merchant_param5=', 'vault=N', 'offer_type=null', 'offer_code=null', 'discount_value=0.0', 'mer_amount=100.0', 'eci_value=null', 'retry=N', 'response_code=0', 'billing_notes=', 'trans_date=17/03/2017 11:27:30'] 
>>> s_dict = {} 
>>> for data in s_list: 
    s_dict[data.split("=")[0]] = data.split("=")[1] 


>>> print s_dict 
{'billing_tel': '1234567890', 'status_code': 'null', 'delivery_country': 'India', 'delivery_name': 'test', 'currency': 'INR', 'delivery_city': 'Pune', 'billing_country': 'India', 'billing_notes': '', 'retry': 'N', 'billing_email': '[email protected]', 'billing_zip': '411041', 'billing_name': 'test', 'merchant_param5': '', 'order_status': 'Success', 'status_message': 'Y', 'mer_amount': '100.0', 'merchant_param3': '', 'merchant_param2': '', 'delivery_zip': '411041', 'card_name': 'AvenuesTest', 'delivery_tel': '1234567890', 'billing_address': 'test', 'order_id': 'BW_225996', 'eci_value': 'null', 'offer_code': 'null', 'merchant_param4': '', 'payment_mode': 'Net Banking', 'offer_type': 'null', 'discount_value': '0.0', 'delivery_address': 'test', 'billing_city': 'Pune', 'merchant_param1': '3', 'response_code': '0', 'failure_message': '', 'bank_ref_no': '1489730168508', 'amount': '100.0', 'trans_date': '17/03/2017 11:27:30', 'tracking_id': '306003083135', 'vault': 'N', 'delivery_state': 'Maharashtra', 'billing_state': 'Maharashtra'} 
+0

謝謝你的回覆 –

+0

非常感謝你的回覆 –

0

一個簡單的循環可以解決這個問題:

a='order_id=BW_225996&tracking_id=306003083135&bank_ref_no=1489730168508&order_status=Success&failure_message=&payment_mode=Net Banking&card_name=AvenuesTest&status_code=null&status_message=Y&currency=INR&amount=100.0&billing_name=test&billing_address=test&billing_city=Pune&billing_state=Maharashtra&billing_zip=411041&billing_country=India&billing_tel=1234567890&[email protected]&delivery_name=test&delivery_address=test&delivery_city=Pune&delivery_state=Maharashtra&delivery_zip=411041&delivery_country=India&delivery_tel=1234567890&merchant_param1=3&merchant_param2=&merchant_param3=&merchant_param4=&merchant_param5=&vault=N&offer_type=null&offer_code=null&discount_value=0.0&mer_amount=100.0&eci_value=null&retry=N&response_code=0&billing_notes=&trans_date=17/03/2017 11:27:30&bin_country=' 

dic={} 
[dic.update({e.split('=')[0] : e.split('=')[1]}) for e in a.split('&')] 
+0

非常感謝你回覆我的問題 –

0

請試試這個簡單的代碼,這將產生您所需的輸出,

input_string = 'order_id=BW_225996&tracking_id=306003083135&bank_ref_no=1489730168508' 

splitted_string = input_string.split('&') 

final_output = {} 

for items in splitted_string: 
    dic_values = items.split('=') 
    final_output[dic_values[0]] = dic_values[1] 

print final_output 

輸出:

{'order_id': 'BW_225996', 'tracking_id': '306003083135', 'bank_ref_no': '1489730168508'} 
  • 最初我們正在使用'&'分割字符串。
  • 然後我們使用'='分割列表中的每個項目作爲字典鍵值和 值。

請讓我知道任何查詢interms。

+0

非常感謝你的回覆 –

+0

你是歡迎.. :) –

0

這是一個可以爲你完成這項工作的功能。它執行以下操作:

  1. 將給定字符串分解爲表示鍵值對的字符串列表。
  2. 將鍵 - 值對的每個字符串表示拆分爲一個鍵及其關聯的值。
  3. 將每個鍵和它的相關值插入dict

parse_dict

def parse_dict(string): 
    dictionary = dict() 
    key_value_pairs = string.split('&') 
    for pair in key_value_pairs: 
     key, value = pair.split('=') 
     dictionary[key] = value 
    return dictionary 

實例:

parsed_dict = parse_dict("order_id=BW_225996&tracking_id=306003083135") 
print(parsed_dict) 

輸出:

{'order_id': 'BW_225996', 'tracking_id': '306003083135'}

我希望這能回答你的問題。快樂的編碼!

+0

非常感謝你的回覆 –

+0

不客氣。這是我的榮幸。 –

0

由於這個問題被標記,這是最短的,最正確的解決辦法:

from django.http import QueryDict 

query_string = 'order_id=BW_225996&tracking_id=306003083135&bank_ref_no=1489730168508' 
qdict = QueryDict(query_string) # dict like object 

不像其他的解決方案,這種處理與URL編碼每個鍵多個值自動(使用getlist())。