简体   繁体   English

在Python中将字符串转换为列表字典

[英]Convert String to Dictionary of Lists in Python

So in my python script I have the following dictionary, except it's listed in string form: 因此,在我的python脚本中,我有以下字典,但它以字符串形式列出:

{'MSVCRT.dll': ['atoi'], 'KERNEL32.DLL': ['VirtualFree', 'ExitProcess', 'VirtualProtect', 'LoadLibraryA', 'VirtualAlloc', 'GetProcAddress'], 'SHLWAPI.dll': ['PathFileExistsA'], 'USER32.dll': ['wsprintfA']}

I however would like to have this code as a dictionary of lists, as it clearly is. 但是,我希望将此代码作为清单的字典使用,很明显。 I tried the following code in orderto attempt to convert the string: 我尝试了以下代码以尝试转换字符串:

    try:
        dictimports = ast.literal_eval(stris)
        print(dictimports)
    except:
        print("dict convert failed")

However it hits the except everytime :( 但是它每次都击中除外:(

So to reiterate, I would like the keys to be say 'KERNEL32.DLL', and then those keys to have the list as the contents of the values, so have a list with the values ['VirtualFree', 'ExitProcess', 'VirtualProtect', 'LoadLibraryA', 'VirtualAlloc', 'GetProcAddress'] in this instance. 因此,我想重申一下,我想把这些键说成“ KERNEL32.DLL”,然后让这些键把列表作为值的内容,所以要有一个带有值的列表['VirtualFree','ExitProcess',' VirtualProtect”,“ LoadLibraryA”,“ VirtualAlloc”,“ GetProcAddress”]。

stris = {'MSVCRT.dll': ['atoi'], 'KERNEL32.DLL': ['VirtualFree', 'ExitProcess', 'VirtualProtect', 'LoadLibraryA', 'VirtualAlloc', 'GetProcAddress'], 'SHLWAPI.dll': ['PathFileExistsA'], 'USER32.dll': ['wsprintfA']}

stris is a dictionary. stris是一本字典。 what seems to be the problem? 似乎是什么问题?

type(stris)

dict 字典

stris.keys()

dict_keys(['MSVCRT.dll', 'KERNEL32.DLL', 'SHLWAPI.dll', 'USER32.dll']) dict_keys(['MSVCRT.dll','KERNEL32.DLL','SHLWAPI.dll','USER32.dll'])

if your stris is a string - in which case you'd have 如果您stris是一个字符串-在这种情况下,你必须

stris  = "{'MSVCRT.dll': ['atoi'], 'KERNEL32.DLL': ['VirtualFree', 'ExitProcess', 'VirtualProtect', 'LoadLibraryA', 'VirtualAlloc', 'GetProcAddress'], 'SHLWAPI.dll': ['PathFileExistsA'], 'USER32.dll': ['wsprintfA']}"

and you will convert it to a dict 然后将其转换为字典

ast.literal_eval(stris)

{'MSVCRT.dll': ['atoi'], 'KERNEL32.DLL': ['VirtualFree','ExitProcess','VirtualProtect','LoadLibraryA','VirtualAlloc', 'GetProcAddress'],'SHLWAPI.dll': ['PathFileExistsA'],'USER32.dll':['wsprintfA']} {'MSVCRT.dll':['atoi'],'KERNEL32.DLL':['VirtualFree','ExitProcess','VirtualProtect','LoadLibraryA','VirtualAlloc','GetProcAddress'],'SHLWAPI.dll' :['PathFileExistsA'],'USER32.dll':['wsprintfA']}

You could use eval() to convert the string to a dict. 您可以使用eval()将字符串转换为字典。

The expression argument is parsed and evaluated as a Python expression expression参数被解析并评估为Python表达式

eval(stris) will execute the executions given as string an in your case return the parsed dictionary. eval(stris)将执行以字符串形式给出的执行,在您情况下返回已解析的字典。

But be aware of this: Using python's eval() vs. ast.literal_eval()? 但是请注意: 使用python的eval()与ast.literal_eval()?

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

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