python - Python脚本将数据从txt提取到csv
问题描述
我正在尝试编写一个 Python 脚本来将 Wi-Fi 数据从 txt 文件提取到 csv
这是txt数据:
Wed Oct 7 09:00:01 UTC 2020
BSS 02:ca:fe:ca:ca:40(on ap0_1)
freq: 2422
capability: IBSS (0x0012)
signal: -60.00 dBm
primary channel: 3
last seen: 30 ms ago
BSS ac:86:74:0a:73:a8(on ap0_1)
TSF: 229102338752 usec (2d, 15:38:22)
freq: 2422
capability: ESS (0x0421)
signal: -62.00 dBm
primary channel: 3
我需要以这种格式将 txt 数据提取到 csv 文件:
Time | BSS | freq |capability |signal| primary channel |
----------------------------+---------------------------+------+-------------+------+-----------------+
Wed Oct 7 09:00:01 UTC 2020|02:ca:fe:ca:ca:40(on ap0_1)| 2422 |IBSS (0x0012)|-60.00| 3 |
|ac:86:74:0a:73:a8(on ap0_1)| 2422 |IBSS (0x0012)|-62.00| 3 |
这是我未完成的代码:
import csv
import re
fieldnames = ['TIME', 'BSS', 'FREQ','CAPABILITY', 'SIGNAL', 'CHANNEL']
re_fields = re.compile(r'({})+:\s(.*)'.format('|'.join(fieldnames)), re.I)
with open('ap0_1.txt') as f_input, open('ap0_1.csv', 'w', newline='') as f_output:
csv_output = csv.DictWriter(f_output, fieldnames= fieldnames)
csv_output.writeheader()
start = False
for line in f_input:
line = line.strip()
if len(line):
if 'BSS' in line:
if start:
start = False
block.append(line)
text_block = '\n'.join(block)
for field, value in re_fields.findall(text_block):
entry[field.upper()] = value
if line[0] == 'on ap0_1':
entry['BSS'] = block[0]
csv_output.writerow(entry)
else:
start = True
entry = {}
block = [line]
elif start:
block.append(line)
当我运行它时,数据没有正确放置。
请让我知道如何解决这个问题。我只是编程的初学者,不胜感激。谢谢你。
解决方案
使用str.startswith
前任:
import csv
fieldnames = ('TIME', 'BSS', 'freq','capability', 'signal', 'primary channel')
with open(filename) as f_input, open(outfile,'w', newline='') as f_output:
csv_output = csv.DictWriter(f_output, fieldnames= fieldnames)
csv_output.writeheader()
result = {"TIME": next(f_input).strip()} #Get Time, First Line
for line in f_input:
line = line.strip()
if line.startswith(fieldnames):
if line.startswith('BSS'):
key, value = line.split(" ", 1)
else:
key, value = line.split(": ")
result[key] = value
csv_output.writerow(result)
根据评论编辑
如果您有多个上述文本块
import re
import csv
week_ptrn = re.compile(r"\b(" + "|".join(('Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun')) + r")\b")
fieldnames = ('TIME', 'BSS', 'freq','capability', 'signal', 'primary channel')
with open(filename) as f_input, open(outfile,'w', newline='') as f_output:
csv_output = csv.DictWriter(f_output, fieldnames= fieldnames)
csv_output.writeheader()
result = [] #Get Time, First Line
for line in f_input:
line = line.strip()
week = week_ptrn.match(line)
if week:
result.append({"TIME": line})
if line.startswith(fieldnames):
if line.startswith('BSS'):
key, value = line.split(" ", 1)
else:
key, value = line.split(": ")
result[-1][key] = value
csv_output.writerows(result)
推荐阅读
- vim - SPF VIM 语法默认开启
- .net - 浏览按钮存储路径作为变量以在其他地方使用
- bash - 使用 bash 和 awk 删除不包含字符串列表之一的行
- java - 将 Android Studio 项目迁移到桌面应用程序
- twitter-bootstrap - Bootstrap 4 Cards - 如何使卡片高度相同以及添加水平填充/间距
- firebase - Firebase 云功能/通知
- r - 包构建/安装期间“找不到对象”
- matlab - 使用 MATLAB 绘制由 ODE 系统定义的函数
- javascript - 按字段查找寄存器
- reactjs - 如果数据已经在客户端,我应该从服务器还是客户端调用?