列表循环和子流程,标准输出

时间:2014-08-13 10:41:48

标签: python linux bash python-2.7

我想在for循环中调用子进程,并将垂直线水平放在列表中,以便用逗号分隔打印它们。 我的代码是这样的;

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()

此刻工作的线路逐行存储数据,我想要的是将我从iwlist扫描输出中选择的线路添加到有时间的线路末端和GPGGA数据。

问题是我是Python的新手,我无法有效地使用列表或解析或子进程。

任何建议都将不胜感激,谢谢。

sudo wlan0 iwlist扫描的输出:

 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

在没有subprocess.call([iwlist and the stuff的情况下运行文件的输出(不包括注释行)

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

1 个答案:

答案 0 :(得分:0)

您可以使用subprocess.check_output获取流程的输出:

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

然后当你打印时:

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

您可以将iwout替换为map(strip, iwout)以删除不必要的空格。

正如您所看到的,我删除了sudo,因为它可能阻止执行请求密码,您不应在脚本中包含sudo调用,除非它们是交互式的,否则您应该使用root权限运行所有命令,然后执行所有不带sudo的调用。