【发布时间】:2018-05-10 16:08:59
【问题描述】:
添加了文本代码,很抱歉给您带来不便,我只是想在我的帖子中包含终端输出。
edit2:删除了所有图像,这是我最新的代码+文本格式的输入/输出,我在输入文件中发现了错误但我无法用代码修复它,末尾有一个完全空的行当我手动编辑文件并退格以将其删除时,该文件代码工作正常,无论如何解决这个问题是我的 csv 文件是由 airodump 自动生成的,我不知道我是否可以控制它们的格式。
#!/usr/bin/env python
import time
import csv
#def nonblank_lines(f):
# for l in f:
# line = l.rstrip()
# if line:
# yield line
with open('root-01.csv', 'r') as csv_file:
csv_reader = csv.reader(csv_file)
with open('station_value.csv', 'w') as station_file:
csv_writer = csv.writer(station_file, delimiter = ',')
for skip in range(4):
next(csv_reader)
for line in csv_reader:
csv_writer.writerow(line)
with open('station_value.csv', 'r') as csv_file2:
# for line in nonblank_lines(csv_file2):
csv_reader2 = csv.reader(csv_file2)
with open('pwr_value.csv', 'w') as pwr_file:
csv_writer2 = csv.writer(pwr_file, delimiter = ',')
for line in csv_reader2:
try:
print(line)
csv_writer2.writerow([line[3]])
except Exception as details:
print('Invalid data:',line,'rejected due to',details)
这是我的文本格式输入文件:
root-01.csv
BSSID, First time seen, Last time seen, channel, Speed, Privacy, Cipher, Authentication, Power, # beacons, # IV, LAN IP, ID-length, ESSID, Key
1C:5F:2B:0A:02:58, 2017-12-02 04:01:06, 2017-12-02 04:02:34, 6, 54, WPA2, CCMP TKIP,PSK, -85, 654, 155, 0. 0. 0. 0, 6, Halbos,
Station MAC, First time seen, Last time seen, Power, # packets, BSSID, Probed ESSIDs
40:40:A7:67:90:0E, 2017-12-02 04:01:16, 2017-12-02 04:02:33, -1, 63, 1C:5F:2B:0A:02:58,
80:3F:5D:F9:1F:AB, 2017-12-02 04:01:22, 2017-12-02 04:02:09, 0, 557, 1C:5F:2B:0A:02:58,
station_value.csv
Station MAC, First time seen, Last time seen, Power, # packets, BSSID, Probed ESSIDs
40:40:A7:67:90:0E, 2017-12-02 04:01:16, 2017-12-02 04:02:33, -1, 63, 1C:5F:2B:0A:02:58,
80:3F:5D:F9:1F:AB, 2017-12-02 04:01:22, 2017-12-02 04:02:09, 0, 557, 1C:5F:2B:0A:02:58,
pwr_value.csv
Power
-1
0
获取 station_value.csv 工作正常,当我想从 statio_value.csv 获取 line[3] 的值并将其写入新文件 pwr_value.csv 时,问题就出现了(据我所知),因为有站文件末尾的额外行与前一行的大小不同。我试图获取堆栈跟踪但无法作为调试工具在我打开堆栈跟踪时一直显示空白区域。
我也不明白为什么即使有错误仍然会产生pwr_value文件?
【问题讨论】:
-
将您的代码发布为文本,而不是图片!
-
顺便说一句,如果您遇到异常,根据定义,它不可能是“成功执行”
-
"如何调试这个?"
标签: python list csv indexoutofboundsexception