【发布时间】:2020-11-21 15:01:57
【问题描述】:
你好,
我需要将这种string 转换为向下dict
string = "OS: Windows 7 SP1, Windows 8.1, Windows 10 (64bit versions only)Processor: Intel Core i5 2400s @ 2.5 GHz, AMD FX 6120 @ 3.5 GHz or betterMemory: 6 GB RAMGraphics: NVIDIA GeForce GTX 660 with 2 GB VRAM or AMD Radeon HD 7870, with 2 GB VRAM or better - See supported List"
DICT
requirements={
'Os':'Windows 7 SP1, Windows 8.1, Windows 10 (64bit versions only)',
'Processor':' Intel Core i5 2400s @ 2.5 GHz, AMD FX 6120 @ 3.5 GHz or better',
'Memory':'6 GB RAM',
'Graphics':'VIDIA GeForce GTX 660 with 2 GB VRAM or AMD Radeon HD 7870, with 2 GB VRAM or better - See supported List',
}
我试过了
string = string.split(':')
并用这样的字典存储每个列表
requirements['Os'] = string[0]
requirements['Processor'] = string[1]
但这不是正确的方法!这给我带来了更多的错误。那么,这些东西是否有任何自定义函数或模块?
【问题讨论】:
-
这将很难格式化,因为您有部分字符串已连接,即
only)Processor:,除非标题的顺序不会改变。它会始终遵循您提供的示例输入吗? -
是的,它总是遵循我为演示给出的示例输入!都是一样的
标签: python python-3.x string dictionary