简体   繁体   English

将自定义字符串转换为字典

[英]Convert Custom String to Dict

HELLO, I need to convert this kind of string to down 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"

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',
}

and i tried this我试过这个

string = string.split(':')

and stored each list with the dict like this并像这样用字典存储每个列表

requirements['Os'] = string[0]
requirements['Processor'] = string[1]

but this is not the right way to do it!但这不是正确的方法! which brings me lot more errors.这给我带来了更多的错误。 So, is there any custom functions or module for these things ?那么,这些东西是否有任何自定义功能或模块?

I'd use a regex to just capture the text that you want, since the actual format of the input string won't be changing.我会使用正则表达式来捕获您想要的文本,因为输入字符串的实际格式不会改变。 This should give you want:这应该给你想要的:


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)

The regex is a little inflexible though and I would advice just using this as a starting point.不过,正则表达式有点不灵活,我建议只使用它作为起点。

See re.match重新匹配

This is an alternative, non-regex solution, though regex may in principle be more efficient and cleaner:这是一种替代的非正则表达式解决方案,尽管正则表达式原则上可能更高效、更干净:

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)

Here the string is first split based on the whitespace, then the code finds the position of each chunk of code that contains the keys-tags of the future dictionary.这里首先根据空格分割字符串,然后代码找到包含未来字典的键标签的每个代码块的位置。 After storing the indices of each key, the code builds the dictionary based on them, with the last element being a special case.在存储每个键的索引后,代码基于它们构建字典,最后一个元素是一个特例。

For all the elements except for last, the code also extracts the information before the next key.对于除 last 之外的所有元素,代码还提取了 next key 之前的信息。 This assumes there is no space between the next key and the last part of the text you want to store for the current key, ie it is always (64bit versions only)Processor: and not (64bit versions only) Processor: - if you cannot make that assumption, you will need to extend this code to cover the cases with a space.这假设在下一个键和要为当前键存储的文本的最后一部分之间没有空格,即它始终是(64bit versions only)Processor:而不是(64bit versions only) Processor: -如果您不能做出这个假设,您将需要扩展此代码以覆盖带有空格的情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM