我将在下面介绍我的一些代码来解释这个问题,但基本上我要做的是在一个函数中进行比较-一个字符串&一个只包含该字符串的行。在
我的名单如下:org_tasks = ['general_static ', 'host_general_static ',etc....]
我用文件名(python文件)传递给函数
^{pr2}$
这就叫:def getTransformedDic(prm_fl,dic_name): # transforms string to dictionary
"turns dictionary values from type variable into a string"
newDic = ""
dicToChange = getOldDicAsString(prm_fl,dic_name)
if dicToChange is None: return None
dicToChange = dicToChange[len(dic_name) + 3:] #here i get none for some reason
dicToChange = dicToChange.replace('"', "'")
for line in dicToChange.splitlines():
transformedLine = ''.join(line.split())
transformedLine = transformedLine.strip()
if transformedLine.startswith('#'): continue
IsLineBelongsToDic = transformedLine.split(':')
if len(IsLineBelongsToDic) > 1:
if transformedLine[-1:] == ",":
valueAsString = '"%s",' % transformedLine[len(IsLineBelongsToDic[0]) + 1:-1]
else:
valueAsString = '"%s",' % transformedLine[len(IsLineBelongsToDic[0]) + 1:]
line = ' ' + IsLineBelongsToDic[0] + ' : ' + valueAsString
newDic += '%s' % line + '\n'
try:
value = ast.literal_eval(newDic[:-1])
except SyntaxError:
return None
else:
return value
def getOldDicAsString(prm_fl, dic_name):
"return a string that contains dictionary"
with open(prm_fl) as f:
file_data = f.read()
recordLines = False
dictionary = ""
for line in file_data.splitlines():
transformedLine = ''.join(line.split())
transformedLine = transformedLine.strip()
taskName = transformedLine.split('=')[0]
print taskName ,dic_name # here i can see they are the same
if taskName == dic_name or recordLines:
recordLines = True
dictionary += line + '\n'
if transformedLine == "}":
return dictionary
我使用的文件如下所示(根据我之前提到的列表):...
CurrentTaskParams.general_static = {
'is_enable' : 'true'
}
CurrentTaskParams.host_general_static = {
'is_enable' : 'true'
}
...
在添加了一些指纹之后,我看到了当我比较CurrentTaskParams.general_static
-->;作为参数传递
到包含此字符串的行(从空格中分条(&;'{&;'='后)
我不附加字典字符串(意味着我的'if'返回false)
任何帮助都会很好,
谢谢!在