如何解析字符串以匹配字典?

时间:2016-06-21 23:08:43

标签: python dictionary

我有以下代码,我尝试设置" build_info_match"仅当所有构建位置出现在" engg_buildinfo"匹配" prod_buildinfo",目前我相信我遇到了错误,因为prod_buildinfo是一个字符串而不是字典,如何从prod_buildinfo获取相应的构建位置?

代码: -

prod_buildinfo = """|h6. {color:blue} Meta Build{color}|{color:blue}MSM1234.LA.1.9{color}|{color:blue}\\flower\location721\INTEGRATION\CI_MSM1234.LA.1.9-116391-STD.INT-2{color}|
|h6. {color:green}MSM1234{color}|
|h6. GLUE|GLUE.MSM1234.2.0|\\bigelow\zipbuild261\INTEGRATION\GLUE.MSM1234.2.0-00010.1-NOOP_TEST-1|
|h6. PASS|LA.HB.1.3.9|\\flower\location674\INTEGRATION\CI_LA.HB.1.3.9-19602-8x96.1-5|
|h6. MOD|MPSS.TH.2.0.2|\\flower\location678\INTEGRATION\MPSS.TH.2.0.2-00089-M1234FAAAANAAM-1|
|h6. BOOT|BOOT.XF.1.0|\\flower\location675\INTEGRATION\BOOT.XF.1.0-00297-M1234LAB-1|
|h6. TZ|TZ.BF.4.0.1|\\flower\location781\INTEGRATION\TZ.BF.4.0.1-00235-M1234AAAAANAAT-1|
|h6. ADSP|ADSP.1234.2.7.1|\\flower\location714\PROD\ADSP.1234.2.7.1-00121-00355-1|
|h6. SLPI|SLPI.HB.1.0|\\flower\location692\INTEGRATION\SLPI.HB.1.0-00277-M1234AAL-1|
|h6. RPM|RPM.BF.1.6|\\flower\location673\TEST\RPM.BF.1.6-00148-M1234AAAAANAAR-1|
|h6. WIR|WIR.RM.4.3|\\flower\location781\INTEGRATION\WIR.RM.4.3-00092-QCARMSWP-1|
|h6. BF|BF.RM.4.3|\\flower\location698\INTEGRATION\BF.RM.4.3-00022-QCABFSWP-1|
|h6. VIDEO|VIDEO.VE.4.2|\\flower\location682\PROD\VIDEO.VE.4.2-00018-PROD-1|
|h6. CPE_V2|CPE.TSF.2.0|\\flower\location670\INTEGRATION\CPE.TSF.2.0-00003-W9335AAAAAAAAQ-1|
|h6. GI|GI.SPR.1.0|\\nickle\zipbuild165\INTEGRATION\GI.SPR.1.0-10011-GISW-1|
|h6. UNKNOWN|WIR.ADDON_RM.3.0|\\nickle\zipbuild193\PROD\WIR.ADDON_RM.3.0-00013-CNSS_RMZ_WAPI-1|"""

engg_buildinfo = {'TZ':'\\gromit\location1\fw.861','PASS':'\\gromit\apps\pass.61'}

build_info_match = False

for key in engg_buildinfo :
  engg_buildinfo['key'] = prod_buildinfo['key']
  #all the build locations in dict match with buildinfo locations
  build_info_match = True


print build_info_match

输出: -

Traceback (most recent call last):
  File "buildinfo.py", line 27, in <module>
    engg_buildinfo['key'] = prod_buildinfo['key']
TypeError: string indices must be integers, not str

1 个答案:

答案 0 :(得分:0)

根据我对你的代码的理解,我想出了这个,我想你可以在这里工作,我可以添加更多的代码,但我不明白你的意思是“dict中的所有构建位置都与buildinfo位置匹配“

import re
prod_buildinfo = """|h6. {color:blue} Meta Build{color}|{color:blue}MSM1234.LA.1.9{color}|{color:blue}\\flower\location721\INTEGRATION\CI_MSM1234.LA.1.9-116391-STD.INT-2{color}|
    |h6. {color:green}MSM1234{color}|
    |h6. GLUE|GLUE.MSM1234.2.0|\\bigelow\zipbuild261\INTEGRATION\GLUE.MSM1234.2.0-00010.1-NOOP_TEST-1|
    |h6. PASS|LA.HB.1.3.9|\\flower\location674\INTEGRATION\CI_LA.HB.1.3.9-19602-8x96.1-5|
    |h6. MOD|MPSS.TH.2.0.2|\\flower\location678\INTEGRATION\MPSS.TH.2.0.2-00089-M1234FAAAANAAM-1|
    |h6. BOOT|BOOT.XF.1.0|\\flower\location675\INTEGRATION\BOOT.XF.1.0-00297-M1234LAB-1|
    |h6. TZ|TZ.BF.4.0.1|\\flower\location781\INTEGRATION\TZ.BF.4.0.1-00235-M1234AAAAANAAT-1|
    |h6. ADSP|ADSP.1234.2.7.1|\\flower\location714\PROD\ADSP.1234.2.7.1-00121-00355-1|
    |h6. SLPI|SLPI.HB.1.0|\\flower\location692\INTEGRATION\SLPI.HB.1.0-00277-M1234AAL-1|
    |h6. RPM|RPM.BF.1.6|\\flower\location673\TEST\RPM.BF.1.6-00148-M1234AAAAANAAR-1|
    |h6. WIR|WIR.RM.4.3|\\flower\location781\INTEGRATION\WIR.RM.4.3-00092-QCARMSWP-1|
    |h6. BF|BF.RM.4.3|\\flower\location698\INTEGRATION\BF.RM.4.3-00022-QCABFSWP-1|
    |h6. VIDEO|VIDEO.VE.4.2|\\flower\location682\PROD\VIDEO.VE.4.2-00018-PROD-1|
    |h6. CPE_V2|CPE.TSF.2.0|\\flower\location670\INTEGRATION\CPE.TSF.2.0-00003-W9335AAAAAAAAQ-1|
    |h6. GI|GI.SPR.1.0|\\nickle\zipbuild165\INTEGRATION\GI.SPR.1.0-10011-GISW-1|
    |h6. UNKNOWN|WIR.ADDON_RM.3.0|\\nickle\zipbuild193\PROD\WIR.ADDON_RM.3.0-00013-CNSS_RMZ_WAPI-1|"""

engg_buildinfo = {'TZ':'\\gromit\location1\fw.861','PASS':'\\gromit\apps\pass.61'}
prod_buildinfo_dict = {}
# Cleanup unwanted color data like {color:blue} or {color}
prod_buildinfo = re.sub(r'{color.{0,6}}', '', prod_buildinfo)

# Split your file by this string "|h6. "
rows = prod_buildinfo.split("|h6. ")
# Remove the first row because it is empty
rows = rows[1::]

# Create a dictionary from your data
for row in rows:
    #split your rows in columns
    columns = row.split("|")

    #Some data would not have location  
    if len(columns) > 2:
        location = columns[2]
    else:
        location = ""

    # Creating the dictionary
    prod_buildinfo_dict[columns[0]] = location