正则表达式来查找搜索项并将结果放入另一个数据文件中?

2024-10-03 15:33:43 发布

您现在位置:Python中文网/ 问答频道 /正文

不确定这个问题的题目是否正确,所以让我给你一些背景资料。我有两个文本文件。一个叫数据.txt另一个打电话来结果.txt. 在数据文件中,我有一个思科网络设备“显示版本”的结果。如下所示:

Cisco IOS Software, s72033_rp Software (s72033_rp-ADVIPSERVICESK9_WAN-M), Version 12.2(33)SXI4, RELEASE SOFTWARE (fc3)
Technical Support: http://www.cisco.com/techsupport
Copyright (c) 1986-2010 by Cisco Systems, Inc.
Compiled Sat 29-May-10 17:54 by prod_rel_team

ROM: System Bootstrap, Version 12.2(17r)SX6, RELEASE SOFTWARE (fc1)

 core-router uptime is 2 years, 5 weeks, 1 day, 5 hours, 47 minutes
Uptime for this control processor is 2 years, 5 weeks, 1 day, 4 hours, 50 minutes
Time since san-qrc1 switched to active is 2 years, 5 weeks, 1 day, 4 hours, 56 minutes
System returned to ROM by reload at 16:12:08 PDT Fri Aug 27 2010 (SP by reload)
System restarted at 16:19:33 PDT Fri Aug 27 2010
System image file is "sup-bootdisk:s72033-advipservicesk9_wan-mz.122-33.SXI4.bin"
Last reload reason: Reload Command



This product contains cryptographic features and is subject to United
States and local country laws governing import, export, transfer and
use. Delivery of Cisco cryptographic products does not imply
third-party authority to import, export, distribute or use encryption.
Importers, exporters, distributors and users are responsible for
compliance with U.S. and local country laws. By using this product you
agree to comply with applicable laws and regulations. If you are unable
to comply with U.S. and local laws, return this product immediately.

A summary of U.S. laws governing Cisco cryptographic products may be found at:
http://www.cisco.com/wwl/export/crypto/tool/stqrg.html

If you require further assistance please contact us by sending email to
export@cisco.com.

cisco WS-C6509-E (R7000) processor (revision 1.5) with 983008K/65536K bytes of memory.
Processor board ID XXXXXXXXXX
SR71000 CPU at 600Mhz, Implementation 0x504, Rev 1.2, 512KB L2 Cache
Last reset from s/w reset
35 Virtual Ethernet interfaces
51 Gigabit Ethernet interfaces
26 Ten Gigabit Ethernet interfaces
1917K bytes of non-volatile configuration memory.
8192K bytes of packet buffer memory.

65536K bytes of Flash internal SIMM (Sector size 512K).
Configuration register is 0x2102

简单地说,我想读数据.txt把绳子拉出来放到结果.txt. 一个CSV格式会很好,但是我很乐意只提取数据。在

例如,该脚本将提取相关数据,如设备的主机名(在本例中是核心路由器)、系统映像文件名(s72033-advipservicesk9_wan-mz.122-33.SXI4.bin)、正常运行时间(2年、5周、1天、5小时、47分钟)、序列号(XXXXXXXX)和型号(WS-C6509-E)。所有这些信息将以制表符分隔的格式放入结果.txt. 在

在未来,不同数据.txt文件可以被利用,并且数据附加到结果.txt,给我一个连续的数据点统计。我希望这有道理。我尝试过搜索我要找的内容,但是我发现的大多数东西要么是在文本文件中查找整行,要么是获取单词出现的索引号。在

最后一件事:根据思科设备的型号,所有项目都会有所不同。它周围的字一般都是一样的,但我要找的东西会不同。如果您能提供任何帮助,我们将不胜感激。提前谢谢。在

更新

我使用了您提供的脚本,但我仍然看到以下内容:

^{pr2}$

问题是主机名缩进了1个空格。当我粘贴它时,压痕消失了。但是,当发出“show version”命令时,会出现1个空格缩进。尝试在上面运行代码会破坏脚本。移除空间可以让它工作。在


Tags: andofto数据txtbyiswith
1条回答
网友
1楼 · 发布于 2024-10-03 15:33:43

您可以将文件读入如下字符串:

with open("data.txt") as infile:
    text = infile.read()

然后可以使用正则表达式提取相关信息:

^{pr2}$

现在match.groups()包含:

^{3}$

您可以使用此命令写入csv文件,如下所示:

import csv
with open("results.txt", "a") as outfile:
    outcsv = csv.Writer(outfile)
    outcsv.writerow(match.groups())

相关问题 更多 >