Python 使用包含的变量重新格式化字符串

Python 使用包含的变量重新格式化字符串,python,variables,robotframework,Python,Variables,Robotframework,我有一个充满变量的库(我不希望手动编辑),它看起来如下: def get_variables(gateway): variables={ 'gateway':gateway, 'license_test_data':gateway['license_data'], 'license_for_application':gateway['license_for_application'], 'Valid_Login_Test_

我有一个充满变量的库(我不希望手动编辑),它看起来如下:

def get_variables(gateway):
    variables={
        'gateway':gateway, 
        'license_test_data':gateway['license_data'],
        'license_for_application':gateway['license_for_application'],
        'Valid_Login_Test_Vairables':{'valid_ssh_command':['ssh '+gateway['DeviceUnderTestUserid']+'@'+gateway['DeviceUnderTestIP'],'password:']}
        #around 3000 more just like this or worse
       }
return variables
gateway['license_test_data'] = "gateway['license_test_data']"
gateway['license_for_application'] = "gateway['license_for_application']"
gateway['license_for_setup'] = "gateway['license_for_setup']"
我需要在不替换“网关”变量的情况下将此信息导入python数据结构。换句话说,我希望能够:

print vars['Valid_Login_Test_Vairables']['valid_ssh_command']
得到这个

'ssh +gateway['DeviceUnderTestUserid']+'@'+gateway['DeviceUnderTestIP'],'password:']
相反,我的结论是:

print vars['Valid_Login_Test_Vairables']['valid_ssh_command']

"ssh gateway['DeviceUnderTestUserid']@gateway['DeviceUnderTestIP']", 'password:'
我一直在尝试的是:

import varfile
import dummy.gateway
vars=varfile.get_variables(dummy.gateway)
我的dummy.gateway看起来像这样:

def get_variables(gateway):
    variables={
        'gateway':gateway, 
        'license_test_data':gateway['license_data'],
        'license_for_application':gateway['license_for_application'],
        'Valid_Login_Test_Vairables':{'valid_ssh_command':['ssh '+gateway['DeviceUnderTestUserid']+'@'+gateway['DeviceUnderTestIP'],'password:']}
        #around 3000 more just like this or worse
       }
return variables
gateway['license_test_data'] = "gateway['license_test_data']"
gateway['license_for_application'] = "gateway['license_for_application']"
gateway['license_for_setup'] = "gateway['license_for_setup']"

如何才能将变量文件的确切内容转换为有用的数据结构?

我发现您的问题有点难以理解,但如果我能够正确地做到这一点,下面将从您所拥有的内容中生成您所希望的内容

# used as a stand-in for your 'import dummy.gateway'
dummy_gateway = {}
dummy_gateway['license_test_data'] = "gateway['license_test_data']"
dummy_gateway['license_for_application'] = "gateway['license_for_application']"
dummy_gateway['license_for_setup'] = "gateway['license_for_setup']"
dummy_gateway['DeviceUnderTestUserid'] = "gateway['DeviceUnderTestUserid']"
dummy_gateway['DeviceUnderTestIP'] = "gateway['DeviceUnderTestIP']"

# your example code corrected and reformatted to be slightly more readable
def get_variables(gateway):
    variables={
        'gateway':gateway,
        'license_test_data':gateway['license_test_data'],
        'license_for_application':gateway['license_for_application'],
        'Valid_Login_Test_Vairables':{
            'valid_ssh_command':
                ['ssh '+gateway['DeviceUnderTestUserid']+'@'+
                    gateway['DeviceUnderTestIP'],
                 'password:']
            }
        #around 3000 more just like this or worse
       }
    return variables

vars = get_variables(dummy_gateway)
print vars['Valid_Login_Test_Vairables']['valid_ssh_command']
结果是一个由两个字符串组成的
列表

[“ssh网关['DeviceUnderTestUserid']@gateway['DeviceUnderTestIP'],'password:']
希望这有帮助


p.S.BTW名称
vars
与标准库中的a名称冲突,因此使用它作为一个变量的名称通常被认为是一种不好的编程实践,即使这样做有效,就本例而言。

您拥有的和想要的都包含语法错误。但除此之外,您应该开始考虑XML或JSON来序列化您的数据。您能修复您的引用吗?这有点混乱,所以我不太理解您的问题。您的第一个示例甚至不是有效的Python。请发布一些实际有效的东西,首先是。larsman,奇怪的布局是机器人框架的要求,我不能改成XML或JSON。vars不是我使用的真正变量,我对这个问题做了一般化。但是我想我在这个问题上不清楚,但是这个练习的目的是避免手工重新编码变量文件。我需要将其读入python数据结构,以便重新格式化。@Skip Huffman:对不起,我误解了。如果您可以显示或至少描述此变量文件中的数据格式,以及您希望将其放入何种数据结构(或询问出于某些特定目的的最佳数据结构),那么您可能会得到更好的帮助。当然。文件的数据结构由一个称为变量的模块组成。本模块描述一个python字典,字典的每个元素可能是字符串、列表或其他字典。这些元素中的每一个都可能在此文件中完全定义,或者可能包括在“网关”字典中导入时传递给变量文件的变量。我希望完整地导入这个变量字典,不替换变量。然后,我可以将这些数据与来自其他几个来源的一些信息结合起来,再次写出一个更新、更好的变量文件。