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