【问题标题】:reformatting strings with included variables使用包含的变量重新格式化字符串
【发布时间】:2012-01-11 18:33:33
【问题描述】:

我有一个充满变量的库(我不想手动编辑),它看起来像这样:

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

我需要将此信息导入 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 看起来像这样:

gateway['license_test_data'] = "gateway['license_test_data']"
gateway['license_for_application'] = "gateway['license_for_application']"
gateway['license_for_setup'] = "gateway['license_for_setup']"

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

【问题讨论】:

  • 您拥有的和想要的都包含语法错误。但除此之外,您应该开始考虑使用 XML 或 JSON 来序列化您的数据。
  • 你能修正你的报价吗?这有点令人困惑,所以我不太了解您的问题。
  • 您的第一个示例甚至不是有效的 Python。请先发布一些实际有效的内容。
  • larsman,奇怪的布局是机器人框架的要求,我不能更改为 XML 或 JSON。

标签: python variables robotframework


【解决方案1】:

我发现你的问题有点难以理解,但如果我能正确地做到这一点,下面会从你所拥有的东西中产生你所说的你想要的东西。

# 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']

结果,即由两个字符串组成的list

["ssh gateway['DeviceUnderTestUserid']@gateway['DeviceUnderTestIP']",'password:']

希望这会有所帮助。

附:顺便说一句,vars 的名称与标准库中 built-in Python function 的名称冲突,因此即使在这种情况下,将其用作变量之一的名称通常也被认为是一种不好的编程习惯,就目前而言顺其自然。

【讨论】:

  • vars 不是我使用的真正变量,我把它作为问题的通用变量。但我想我并不清楚这个问题,但这个练习的重点是避免手动重新编码变量文件。我需要将它读入 python 数据结构,以便我可以重新格式化它。
  • @Skip Huffman:对不起,我误解了。如果您可以显示或至少描述此变量文件中数据的格式,以及您希望将其放入哪种数据结构(或询问对于某些特定目的什么是最好的),那么也许您会得到更好的帮助。
  • 当然可以。该文件的数据结构由一个称为变量的模块组成。这个模块描述了一个python字典,字典的每个元素可能是一个字符串、列表或另一个字典。这些元素中的每一个都可能在此文件中完全定义,或者可能包含在“网关”字典中的导入时传递给变量文件的变量。我希望完整地导入这个变量字典,而变量没有被替换。然后,我可以将这些数据与来自其他几个来源的一些信息结合起来,并再次写出一个更新、更好的变量文件。
猜你喜欢
  • 2016-06-17
  • 1970-01-01
  • 1970-01-01
  • 2012-10-24
  • 1970-01-01
  • 2017-03-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多