【问题标题】:How do I add the below string to a dictionary in key value pair format [duplicate]如何以键值对格式将以下字符串添加到字典中[重复]
【发布时间】:2021-05-11 22:53:53
【问题描述】:

输入:

"name=xyz,city=bangalore,company=abc,education=BE"

输出:

{"name":"xyz", "city":"bangalore", "company":"abc", "education":"B.E"}

我可以得到['name=xyz', 'city=bangalore', 'company=abc', 'education=BE'] 但我坚持拆分列表中的单词并附加到字典。

【问题讨论】:

  • 再分一次?
  • 你是怎么把它变成list的?如果您在= 上拆分,则应使用相同的过程将其放入dict
  • @Tomerikoo s.split(',')?你错过了s...

标签: python python-3.x


【解决方案1】:

您可以尝试以下方法:

inp = "name=xyz,city=bangalore,company=abc,education=BE"

out = dict(x.split("=") for x in inp.split(","))
# {'name': 'xyz', 'city': 'bangalore', 'company': 'abc', 'education': 'BE'}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-04-16
    • 2019-02-14
    • 2014-04-13
    • 1970-01-01
    • 1970-01-01
    • 2022-10-13
    • 1970-01-01
    • 2013-10-10
    相关资源
    最近更新 更多