[英]Convert Custom String to Dict
你好,我需要将这种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"
字典
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]
但这不是正确的方法! 这给我带来了更多的错误。 那么,这些东西是否有任何自定义功能或模块?
我会使用正则表达式来捕获您想要的文本,因为输入字符串的实际格式不会改变。 这应该给你想要的:
import re
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"
matches = re.match(r'OS: (.+)Processor: (.+)Memory: (.+)Graphics: (.+)', string)
requirements = {
'Os': matches.group(1),
'Processor': matches.group(2),
'Memory': matches.group(3),
'Graphics': matches.group(4),
}
print(requirements)
不过,正则表达式有点不灵活,我建议只使用它作为起点。
见重新匹配
这是一种替代的非正则表达式解决方案,尽管正则表达式原则上可能更高效、更干净:
input_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"
# Splits by space
input_string = input_string.split()
# Assumes the keys are exactly like listed - including uppercase letters
key_list = ["OS", "Processor", "Memory", "Graphics"]
key_ind = []
output = {}
# Collect indices corresponding to each key
for key in key_list:
for idx, el in enumerate(input_string):
if key in el:
key_ind.append(idx)
break
# Build the dictionary
for idx, key in enumerate(key_list):
if idx + 1 >= len(key_list):
output[key] = (' ').join(input_string[key_ind[idx]+1:])
else:
lp_idx = input_string[key_ind[idx+1]].find(key_list[idx+1])
lp = input_string[key_ind[idx+1]][:lp_idx]
output[key] = (' ').join(input_string[key_ind[idx]+1:key_ind[idx+1]]) + ' ' + lp
print(output)
这里首先根据空格分割字符串,然后代码找到包含未来字典的键标签的每个代码块的位置。 在存储每个键的索引后,代码基于它们构建字典,最后一个元素是一个特例。
对于除 last 之外的所有元素,代码还提取了 next key 之前的信息。 这假设在下一个键和要为当前键存储的文本的最后一部分之间没有空格,即它始终是(64bit versions only)Processor:
而不是(64bit versions only) Processor:
-如果您不能做出这个假设,您将需要扩展此代码以覆盖带有空格的情况。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.