Py学习  »  Python

如何在python中将一串键值转换为正确的dict?

eatkimchi • 3 年前 • 1770 次点击  

假设我有一本字典 一串 中间有未知数量的空格:

'address: 123 fake street city: new york state: new york population: 500000'

我该怎么去

{'address': '123 fake street',
 'city': 'new york',
 'state': 'new york',
 'population': 500000}

甚至是列表或元组,其效果如下:

['address', '123 fake street'],
['city', 'new york'...]

(1) 假设键总是带有冒号的单字

key:

city:

population:

(2) 假设边缘情况 Address: 可能是“X栋S/W约翰·史密斯转交”

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/133799
 
1770 次点击  
文章 [ 5 ]  |  最新文章 3 年前