Stripping non-numbers from a file parse without nested lists?

daku9999 at daku9999 at
Wed Apr 1 08:35:27 CEST 2009

On Mar 31, 6:47 pm, "Rhodri James" <rho... at>

> What you're doing (pace error checking) seems fine for the data
> structures that you're using.  I'm not entirely clear what your usage
> pattern for "dip" and "dir" is once you've got them, so I can't say
> whether there's a more appropriate shape for them.  I am a bit curious
> though as to why a nested list is non-ideal?
> ...
>      if "/" in word and "dip" not in word:
>         dip_n_dir.append(word.split("/", 1))
> is marginally shorter, and has the virtue of making it harder to use
> unrelated dip and dir values together.
> --
> Rhodri James *-* Wildebeeste Herder to the Masses


Thanks.  That works better than what I had before and I learned a new
method of parsing what I was looking for.

Now I'm on to jumping a set number of lines from a given positive
search match:

...(lines of garbage)...
5656      (or some other value I want, but don't explicitly know)
...(18 lines of garbage)...
search object
...(lines of garbage)...

I've tried:

def read_poles(filename):
  index = 0
  fh = None
      fh = open(filename, "r")
      while True:

          if "search object" in lines[index]
              poles = int(lines[index-18])

          index +=1

  except(IndexError): pass

      if fh is not None: # close file


Which half works.  If it's not found, IndexError is caught and passed
(avoids quitting on lines[index out of range].  The print(poles)
properly displays the value I am looking for (_always_ 18 lines before
the search object).

However, since it is assigned using the index variable, the value of
poles doesn't keep (poles is always zero when referenced outside of
the read_poles function).  I'm assuming because I'm pointing to a
certain position of an object and once index moves on, it no longer
points to anything valid.  My python book suggested using
copy.deepcopy, but that didn't get around the fact I am calling it on

Any experience jumping back (or forward) a set number of lines once a
search object is found?  This is the only way I can think of doing it
and it clearly has some problems.

Reading the file line by line using for line in blah works for finding
the search object, but I can't see a way of going back the 18 lines to
grabbing what I need.

Thanks for the help!  I'm slowly getting this mangled mess of a file
into something automated (hand investigating the several thousand
files I need to do would be unpleasant).

More information about the Python-list mailing list