handling command output in python
Question: I want to work with the output of a wifi scan command. The output is several
lines and I am interested in 2 information out of it. The goal is to have the
ESSID and the address in a two dimmension array (hope thats right?) Here is
what I got so far:

    
    
    #!/usr/bin/python
    import subprocess
    import re
    from time import sleep
    
    # set wifi interface
    wif = "wlan0"
    

So I get the command stdout and I find out that to work with this output in a
loop I have to use iter

    
    
    # check for WiFis nearby
    wifi_out = subprocess.Popen(["iwlist", wif ,"scan"],stdout=subprocess.PIPE)
    wifi_data = iter(wifi_out.stdout.readline,'')
    

Then I used enumerate to have the index and therefore I search for the line
with the address and the next line (index + 1) would contain the ESSID

    
    
    for index, line in enumerate(wifi_data):
        searchObj = re.search( r'.* Cell [0-9][0-9] - Address: .*', line, re.M|re.I)
        if searchObj:
            print index, line
            word = line.split()
            wifi = [word[4],wifi_data[index + 1]]
    

Now I have two problems  
1) wifi_data is the wrong Type  
`TypeError: 'callable-iterator' object has no attribute '__getitem__'`  
2) I guess with  
`wifi = [word[4],wifi_data[index + 1]]`  
I set the the variable every time new instead of have something that appends.
But I want a variable that in the and has all ESSIDs together with all
corresponding addresses. I am new with python, so currently I imaging
something like

    
    
    WIFI[0][0] returns ESSID
    WIFI[0][1] returns address to ESSID in WIFI[0][0]
    WIFI[1][0] returns next ESSID
    WIFI[1][1] returns address to ESSID in WIFI[1][0]
    

and so on. Or would be something else in python better to work with such kind
of information?


Answer: I think you want

    
    
    next(wifi_data)
    

since you cannot index into an iterator ... this will give you the next item
... but it may screw up your loop ...

although really you could just do

    
    
    wifi_out = subprocess.Popen(["iwlist", wif ,"scan"],stdout=subprocess.PIPE)
    wifi_data = wifi_out.communicate()[0].splitlines() 
    

or even easier perhaps

    
    
    wifi_data = subprocess.check_output(["iwlist",wif,"scan"]).splitlines()
    

and then you will have a list ... which will work more like you expect with
regards to accessing the data via index (theres not really a good reason to
use an iter for this that I can tell)

