I have a string that I would like to convert to dict type in python.
Sample string:
'projectid=871106/interactionmonth=201701'
Expected 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)
OUPUT:
{'projectid': 871106, 'interactionmonth': 20170}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.