[英]How to convert string to a dict type in python
我有一個字符串,我想在 python 中轉換為 dict 類型。
示例字符串:
'projectid=871106/interactionmonth=201701'
預期 output:
{"projectid":871106,"interactionmonth":201701}
input = 'projectid=871106/interactionmonth=201701'
key = ""
value = ""
switch = False
result = {}
for character in range(len(input)):
if input[character] == "=": switch = True
elif input[character] == "/" or character == len(input) - 1:
result[key] = int(value)
switch = False
key = ""
value = ""
else:
if (switch): value += input[character]
else : key += input[character]
print(result)
輸出:
{'projectid': 871106, 'interactionmonth': 20170}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.