Guest User

parse_asf.py

a guest
May 6th, 2013
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.53 KB | None | 0 0
  1. """
  2. Extracting information from a Massive Simulation File (asf file)
  3. https://groups.google.com/d/msg/python_inside_maya/_jm4m-bSbUQ/1XUgdPbz26EJ
  4. """
  5.  
  6. def parse_apf(filename):
  7.     agents = {}
  8.     name = ''
  9.  
  10.     # a 'with' context will make sure
  11.     # to close the file no matter what
  12.     # when the 'with' block is done
  13.     with open(filename) as f:
  14.        
  15.         # you can loop over a file handle
  16.         # to get each line
  17.         for line in f:
  18.             line = line.strip()
  19.             # we dont care about blank lines
  20.             if not line:
  21.                 continue
  22.  
  23.             # just split the line on white space
  24.             # into a list
  25.             tokens = line.split()
  26.  
  27.             # are we starting a new agent?
  28.             if line.startswith("BEGIN"):
  29.                 # the name value is hte last item in the list
  30.                 name = tokens[-1]
  31.                 # and start a fresh dict to store the attributes
  32.                 agents[name] = {}
  33.  
  34.             # otherwise we are parsing attributes for
  35.             # the last name we saw
  36.             else:
  37.                 attr = tokens[0]
  38.                 # list comprehension:
  39.                 # convert each string value into a float
  40.                 vals = [float(val) for val in tokens[1:]]
  41.                 # use the name of the last parsed agent
  42.                 # and store the attribute as the key,
  43.                 # and the list of floats as the value
  44.                 agents[name][attr] = vals
  45.  
  46.     return agents
  47.  
  48.  
  49. agents = parse_apf("frame.1.apf")
Advertisement
Add Comment
Please, Sign In to add comment