# read a namelist file into a dictionary # methods are well documented, magic is not import sys namelistD = {} namelistF = open(sys.argv[1]) lineL = namelistF.readlines() lineL = lineL[1:-1] # eliminate first and last line #print lineL for lineS in lineL: lineS = lineS[:-2] # string is immutable, so replace it # print lineS kv = lineS.split("=") key = kv[0] value = kv[1] # print key, value if key == "tprobe" or key == "rprobe": value = value.replace("," , " ") # remove the commas xyz = value.split() print xyz namelistD[key] = xyz else: namelistD[key] = value print "\n-----------------Dictionary { } ------------------" print namelistD # see 5.8 Mapping Types -- dict itemsL = namelistD.items() # returns list of 2-tuples; note parentheses print "\n-----------------List of 2-tuples [ (), () ] ------------------" print itemsL itemsL.sort() # sort alphabetically by first part of tuple print "\n-----------------Sorted List of 2-tuples [ (), () ] ------------------" print itemsL # rprobe and tprobe have a value that is a list of 3 strings # String methods are section 5.6.1 in docs.python.org/2/library/stdtypes.html # bookmark for commonly referenced documentation """ in interpreter: >>> help(str) """