Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- Extracting information from a Massive Simulation File (asf file)
- https://groups.google.com/d/msg/python_inside_maya/_jm4m-bSbUQ/1XUgdPbz26EJ
- """
- def parse_apf(filename):
- agents = {}
- name = ''
- # a 'with' context will make sure
- # to close the file no matter what
- # when the 'with' block is done
- with open(filename) as f:
- # you can loop over a file handle
- # to get each line
- for line in f:
- line = line.strip()
- # we dont care about blank lines
- if not line:
- continue
- # just split the line on white space
- # into a list
- tokens = line.split()
- # are we starting a new agent?
- if line.startswith("BEGIN"):
- # the name value is hte last item in the list
- name = tokens[-1]
- # and start a fresh dict to store the attributes
- agents[name] = {}
- # otherwise we are parsing attributes for
- # the last name we saw
- else:
- attr = tokens[0]
- # list comprehension:
- # convert each string value into a float
- vals = [float(val) for val in tokens[1:]]
- # use the name of the last parsed agent
- # and store the attribute as the key,
- # and the list of floats as the value
- agents[name][attr] = vals
- return agents
- agents = parse_apf("frame.1.apf")
Advertisement
Add Comment
Please, Sign In to add comment