Forum Discussion

John_Laird's avatar
John_Laird
Contributor
8 years ago
Solved

issue using dictionary

I having issues with using dictionaries in TC12 (python). When I add a key/value pair and then attempt a lookup via the key, I always get None (aka null). To be more specific, here is my script:

 

def ParsePropValForCSV(propvalList):
  csvPropValDict = Sys.OleObject["Scripting.Dictionary"]
  for propvalLine in propvalList:
    splitline = propvalLine.split(',')
    if splitline[0] == "CSV":
      propname = str(splitline[1])
      propval = str(splitline[2])
      csvPropValDict.Add(propname, propval)
  blah = csvPropValDict.Item["Prop_File"]
  return csvPropValDict

Through the use of breakpoints I have confirmed that propname has the string "Prop_File" and propval has the string "$$Source.Name$$_$$Meter.ObjectName$$_$$Archive.TypeName$$.csv". The problem is that 'blah' (which should reference the value for key "Prop_File") is always None. Can't figure out why.

 

 

At first I attempted to use Python dictionary syntax but that did not work so I went to this article thinking it would work (no joy): https://support.smartbear.com/testcomplete/docs/scripting/dictionary-object.html

 

I must be doing something wrong here just can't pin it down. Suggestions?

  • Hi John,

     

    Replace

    propvalLine.split(',')

    with

    propvalLine.split(', ')  # note the space

    Your propvalLine contains spaces after commas -- "CVS, PropFile, ..." so if splitting just by commas the key ends up being " PropFile" (with a leading space) instead of "PropFile".

     

    Alternatively, consider using Python's built-in CSV reader, which can handle delimiters and extra spaces for you:

    import csv
    
    with open('C:\myfile.csv') as csvfile:
      reader = csv.reader(csvfile, skipinitialspace = True)
      dict = { line[1]: line[2] for line in reader if line[0] == 'CSV' }
    
    Log.Message( dict['PropFile'] )

4 Replies

  • HKosova's avatar
    HKosova
    SmartBear Alumni (Retired)

    Hi John,

     

    Replace

    propvalLine.split(',')

    with

    propvalLine.split(', ')  # note the space

    Your propvalLine contains spaces after commas -- "CVS, PropFile, ..." so if splitting just by commas the key ends up being " PropFile" (with a leading space) instead of "PropFile".

     

    Alternatively, consider using Python's built-in CSV reader, which can handle delimiters and extra spaces for you:

    import csv
    
    with open('C:\myfile.csv') as csvfile:
      reader = csv.reader(csvfile, skipinitialspace = True)
      dict = { line[1]: line[2] for line in reader if line[0] == 'CSV' }
    
    Log.Message( dict['PropFile'] )
    • John_Laird's avatar
      John_Laird
      Contributor

      Problem resolved! Changed code to truncate leading spaces. Always the simple stuff that gets overlooked and magnified. Thanks to both of you. Revised code if anyone else has the same issue:

       

      def ParsePropValForCSV(propvalList):
        csvPropValDict = Sys.OleObject["Scripting.Dictionary"]
        for propvalLine in propvalList:
          splitline = propvalLine.split(',')
          if splitline[0] == "CSV":
            propname = str(splitline[1]).lstrip(' ')
            propval = str(splitline[2]).lstrip(' ')
            csvPropValDict.Add(propname, propval)
        blah = csvPropValDict.Item['Prop_File']
        return csvPropValDict
  • sanjay0288's avatar
    sanjay0288
    Frequent Contributor

    Hi,

      Please change this line of your code  blah = csvPropValDict.Item["Prop_File"] to

     

    blah = csvPropValDict.Item[propname]

     

    OR

     

    blah = csvPropValDict.Item['Prop_File']

     

    • sanjay0288's avatar
      sanjay0288
      Frequent Contributor

      Hi

      John_Laird Please find my comments

       

        Please change this line of your code  blah = csvPropValDict.Item["Prop_File"] to

       

      blah = csvPropValDict.Item[propname]

       

      OR

       

      blah = csvPropValDict.Item['Prop_File']