[Tutor] Newbie with question about text file processing

Kirby Urner urnerk@qwest.net
Mon, 22 Apr 2002 08:35:08 -0400


> Any help will be greatly appreciated.
> 
> - Stuart

Python can read these files into lists of strings.
If the lines you care about all have a colon, you
can skip lines that don't have one:

f = open("newfile.txt")
data = [i for i in f.readlines() if ":" in i]

then you can strip out what's to the left and right
of the colon:

valdict = {}
for d in data:
   colon = d.index(":")
   left,right = d[:colon].strip(), d[colon+1:].strip()
   valdict[left] = right

Now you can lookup values you care about using this 
dictionary, e.g. valdict['DNS'] will be '99.99.91' or
whatever.  Parse in both files like this, and compare
newvalues['DNS'] with oldvalues['DNS'] -- stuff like
that.

Put these pieces together and you've got at least one 
way of doing it.

Kirby