Forum Discussion

John_Laird's avatar
John_Laird
Contributor
8 years ago

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:   ...
  • HKosova's avatar
    8 years ago

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