# Read all lines from a text file # Parse each line into a key-value pair # Store pair in a dictionary # Write new file with time-stamped key-value pairs import sys inF = open(sys.argv[1], "r") # open the file specified on the command line linesL = inF.readlines() # read all lines of text into a list of Strings inF.close() # no longer needed #kvD = {} # create an empty dictionary; will not preserve order of pairs from collections import OrderedDict kvD = OrderedDict() for lineS in linesL: # iterate through each line of text in the list wL = lineS.split() # parse the line into words keyS = wL[0] # first word is the key valueS = wL[2] # third word is the value, assume w[1] is = print keyS, valueS kvD[keyS] = valueS # add key-value pair to dictionary; all items are strings print " " print kvD.keys() print kvD.values() print " " print kvD.viewitems() import datetime outF = open("log", "w") # open new file; will over-write existing file for k in kvD: # iterate through each key in dictionary v = kvD[k] # logTime = datetime.datetime.now() s = str(logTime) + ": " + k + " " + v + "\n" outF.write(s) outF.close()