Reading and formatting issues in python
Question: New to the site, hopefully my question isn't missing to many details or being
too vague. Essentially i'm running a netbackup script, to create a new file:

    
    
    subprocess.call('/usr/openv/netbackup/bin/goodies/available_media > /var/log/nbu/available_media' + adate, shell=True)
    

[![The output of this file looks like
this](http://i.stack.imgur.com/6tBwj.png)](http://i.stack.imgur.com/6tBwj.png)

I want to then format this and input it into another file using out.write,
that specifies the pool name e.g "Auth Pool", "Web Pool" etc. Use count to
find out how many tapes are in a state of "AVAILABLE" "FULL" "FROZEN" for each
pool.

    
    
    #Pool #Full #Avail #Frozen
    AUTH  100   5      23
    

All i've managed so far is the following:

    
    
    from datetime import datetime
    import subprocess
    import os
    import sys
    
    date = datetime.now()
    adate = '%s%s%s' % (date.year, date.month, date.day)
    
    subprocess.call('/usr/openv/netbackup/bin/goodies/available_media >    /var/log/nbu/available_media' + adate, shell=True)
    
    log = open('/var/log/nbu/available_media' + adate, 'r')
        text = log.read()
        auth = text.split('Auth_Offsite', 1)[0]
        notapes = auth.count('AVAILABLE')
    

Currently my knowledge restricts me to just counting AVAILABLE tapes for the
first pool by using split. I'm new to python so please be gentle :P All help
is greatly appreciated.


Answer: Here's a quick and dirty that should roughly do what you want, assuming that
your log file is tab separated. If you're new to python, some of the functions
might be new to you, but it should give you some ideas about what you might
want to google for at least.

    
    
    log = open('/var/log/nbu/available_media' + adate, 'r')
    
    # Splits on newline chars, skips the titles and ----- lines
    lines = log.readlines()[2:] 
    
    # Break up each line by field
    lines = [line.strip().split('\t') for line in lines]
    
    # Going to form a dictionary mapping pool names to dictionaries of tape state counts
    states_dict = {}
    all_states = set()
    for line in lines:
        if len(line) == 1:
            current_pool = line[0]
            states_dict[current_pool] = {}
        elif len(line) > 1:
            state = line[-1]
            states_dict[current_pool][state] = states_dict[current_pool].setdefault(state, 0) + 1
            all_states.add(state)
    
    all_states = sorted(list(all_states))
    
    # write output
    with open('output_file_name.txt','wb') as outfile:
        outfile.write('Pool\t' + '\t'.join(all_states) + '\n')
        for pool in states_dict:
            outfile.write(pool)
            for state in all_states:
                outfile.write('\t' + states_dict[pool].setdefault(state, 0))
            outfile.write('\n')
    

