List in a Loop and Subprocesses, Standard Output
Question: I want to call a subprocess in a for loop and put the vertical lines
horizontally in a list for printing them by separating by comma. My code is
like this ;

    
    
    import serial
    import time
    import subprocess                                                               #test
    subprocess.call(['echo','I am Learning to use Subprocesses'])   #when Using subprocess.call([*1,*2])   *1 is the first argument and it is the bash code, *2 is the opti$
    ser = serial.Serial("/dev/ttyUSB0", 4800, timeout = 1)
    file = open("/home/pi/GPSWIFIMODIFIED.csv", "a")
    file.write('\n')
    
    for i in range(0,50):
           date = time.time()
           val = ser.readline();
           if val.find("GPGGA")==-1: continue
    ####      subprocess.call([iwlist, scan and the stuff
    ###       iwlout = sys.stdout(((( output on the screen)))))) the output which iwlist wlan0 scan will put on the screen
    ###       line = iwlout.readline()
    ###       for j in range (1,40):
    ###             if line.find("Cell +j")==-1:
    ####                    mylistofwifi = [line.find("Address"),line.find("Quality"),line.find("IEEE"),line.find("Pairwise")]
    
    
    ####    ########################### ######Here there must be iwlist scan, and the gathering must be done
    
           print >> file ,date,',',val[:42],',','WIFI DATA WILL BE HERE'#     print mylistofwifi will be here
    file.close()
    

The lines which works at the moment stores the data line by line and what I
want is to add the lines that I choose from iwlist scan outputs to the end of
the lines that has time and the GPGGA data.

The problem is that I am new to Python and I am not able to use lists or
parsing or the subprocesses efficiently.

Any suggestions will be appreciated, thanks in advance.

**The output from`sudo wlan0 iwlist` scan :**

    
    
     Cell 01 - Address: 00:26:99:4D:0F:34
                        Channel:11
                        Frequency:2.462 GHz (Channel 11)
                        Quality=18/100  Signal level=18/100
                        Encryption key:on
                        ESSID:"STAFF"
                        Bit Rates:1 Mb/s; 2 Mb/s; 5.5 Mb/s; 6 Mb/s; 9 Mb/s
                                  11 Mb/s; 12 Mb/s; 18 Mb/s
                        Bit Rates:24 Mb/s; 36 Mb/s; 48 Mb/s; 54 Mb/s
                        Mode:Master
                        Extra:tsf=0000000db0b79259
                        Extra: Last beacon: 0ms ago
                        IE: Unknown: 00055354414646
                        IE: Unknown: 010882040B0C12161824
                        IE: Unknown: 03010B
                        IE: Unknown: 0706494520010D14
                        IE: Unknown: 0B0504000E8D5B
                        IE: Unknown: 2A0100
                        IE: IEEE 802.11i/WPA2 Version 1
                            Group Cipher : CCMP
                            Pairwise Ciphers (1) : CCMP
                            Authentication Suites (2) : 802.1x Proprietary
                        IE: Unknown: 32043048606C
                        IE: Unknown: 851E06008F000F00FF03590045452D472D303100000000000000000004000027
                        IE: Unknown: 9606004096000E00
                        IE: Unknown: DD180050F2020101800003A4000027A4000042435E0062322F00
                        IE: Unknown: DD06004096010104
                        IE: Unknown: DD050040960305
                        IE: Unknown: DD050040960B09
                        IE: Unknown: DD050040961401
              Cell 02 - Address: 00:26:99:4D:0D:35
                        Channel:11
                        Frequency:2.462 GHz (Channel 11)
                        Quality=21/100  Signal level=21/100
                        Encryption key:on
                        ESSID:"eduroam"
                        Bit Rates:1 Mb/s; 2 Mb/s; 5.5 Mb/s; 6 Mb/s; 9 Mb/s
                                  11 Mb/s; 12 Mb/s; 18 Mb/s
                        Bit Rates:24 Mb/s; 36 Mb/s; 48 Mb/s; 54 Mb/s
                        Mode:Master
                        Extra:tsf=000001021e097bd4
                        Extra: Last beacon: 0ms ago
                        IE: Unknown: 0007656475726F616D
                        IE: Unknown: 010882848B0C12961824
                        IE: Unknown: 03010B
                        IE: Unknown: 0706494520010D14
                        IE: Unknown: 0B0503000C8D5B
                        IE: Unknown: 2A0100
                        IE: IEEE 802.11i/WPA2 Version 1
                            Group Cipher : TKIP
                            Pairwise Ciphers (2) : TKIP CCMP
                            Authentication Suites (2) : 802.1x Proprietary
                        IE: Unknown: 32043048606C
                        IE: Unknown: 851E02008F000F00FF03590045452D472D303200000000000000000003000027
                        IE: Unknown: 9606004096000B00
                        IE: WPA Version 1
                            Group Cipher : TKIP
                            Pairwise Ciphers (1) : TKIP
                            Authentication Suites (2) : 802.1x Proprietary
                        IE: Unknown: DD180050F2020101800003A4000027A4000042435E0062322F00
                        IE: Unknown: DD06004096010104
                        IE: Unknown: DD050040960305
                        IE: Unknown: DD050040960B09
                        IE: Unknown: DD050040961401
              Cell 03 - Address: 00:26:99:4D:0D:30
                        Channel:11
                        Frequency:2.462 GHz (Channel 11)
                        Quality=21/100  Signal level=21/100
                        Encryption key:on
                        ESSID:"CONF"
                        Bit Rates:1 Mb/s; 2 Mb/s; 5.5 Mb/s; 6 Mb/s; 9 Mb/s
                                  11 Mb/s; 12 Mb/s; 18 Mb/s
                        Bit Rates:24 Mb/s; 36 Mb/s; 48 Mb/s; 54 Mb/s
                        Mode:Master
                        Extra:tsf=000001021e0e36dd
                        Extra: Last beacon: 0ms ago
                        IE: Unknown: 0004434F4E46
                        IE: Unknown: 010882848B0C12961824
                        IE: Unknown: 03010B
                        IE: Unknown: 0706494520010D14
                        IE: Unknown: 0B0503000C8D5B
                        IE: Unknown: 2A0100
                        IE: Unknown: 32043048606C
                        IE: Unknown: 851E02008F000F00FF03590045452D472D303200000000000000000003000027
                        IE: Unknown: 9606004096000B00
                        IE: WPA Version 1
                            Group Cipher : TKIP
                            Pairwise Ciphers (1) : TKIP
                            Authentication Suites (1) : PSK
                        IE: Unknown: DD180050F2020101800003A4000027A4000042435E0062322F00
                        IE: Unknown: DD06004096010104
                        IE: Unknown: DD050040960305
                        IE: Unknown: DD050040960B09
                        IE: Unknown: DD050040961400
    

**The output of running the file without the`subprocess.call([iwlist and the
stuff` (not including the commented lines)**

    
    
    1407868084.56 , $GPGGA,104023.000,5323.0922,N,00636.1480,W , WIFI DATA WILL BE HERE
    1407868085.21 , $GPGGA,104024.000,5323.0922,N,00636.1480,W , WIFI DATA WILL BE HERE
    1407868086.21 , $GPGGA,104025.000,5323.0922,N,00636.1480,W , WIFI DATA WILL BE HERE
    1407868087.62 , $GPGGA,104026.000,5323.0922,N,00636.1480,W , WIFI DATA WILL BE HERE
    1407868088.25 , $GPGGA,104027.000,5323.0922,N,00636.1480,W , WIFI DATA WILL BE HERE
    1407868089.21 , $GPGGA,104028.000,5323.0922,N,00636.1480,W , WIFI DATA WILL BE HERE
    1407868090.21 , $GPGGA,104029.000,5323.0922,N,00636.1480,W , WIFI DATA WILL BE HERE
    1407868091.2 , $GPGGA,104030.000,5323.0922,N,00636.1480,W , WIFI DATA WILL BE HERE
    1407868092.61 , $GPGGA,104031.000,5323.0922,N,00636.1480,W , WIFI DATA WILL BE HERE
    


Answer: You can use
[`subprocess.check_output`](https://docs.python.org/2/library/subprocess.html#subprocess.check_output)
to get the output of a process:

    
    
    from subprocess import check_output
    iwout = check_output(['wlan0', 'iwlist', 'scan']).splitlines()
    

And then when you print:

    
    
    print >> file, date, ',', val[:42], ',', ', '.join(iwout)
    

You can replace `iwout` with `map(strip, iwout)` to remove the unnecessary
whitespaces.

As you can see I removed the `sudo`, because it could block the execution
asking for the password, you shouldn't include a `sudo` call in your script
unless they are interactive, otherwise you should run all the command with
root privileges, and then perform all the invocations without `sudo`.

