python - 循环和子流程中的列表,标准输出

标签 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 和内容) 运行文件的输出(不包括注释行)

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

最佳答案

您可以使用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 的情况下执行所有调用。

关于python - 循环和子流程中的列表,标准输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25284060/

相关文章:

linux - IPC - 如何将命令输出重定向到子进程中的共享内存段

linux - Shell - 将环境变量与 ccrypt 一起使用

python - 看不懂Python表达式

python - 如何检查一个字符串是否以另一个字符串结尾,或者相反?

python - 如果 Python Pandas 中的列包含任何特殊字符,则重复行

bash - 为什么要在 .profile 脚本中转义 eval 的第一个字符?

bash - 如何在 whiptail 进度条上显示不同的消息以及进度条?

python - pyodbc 在版本 3+ 上是否存在编码问题?

c - 多端口服务器未获得所有传输

c# - 在 Ubuntu VPS (14.04.1 LTS) 上使用单声道,为什么我的 HTTPS HttpListener 从未收到上下文?