找到逻辑 ID 和 IP 地址后,如何使此代码生成新行?

Posted

技术标签:

【中文标题】找到逻辑 ID 和 IP 地址后,如何使此代码生成新行?【英文标题】:How can I make this code generate a new line after finding Logical ID and IP Address? 【发布时间】:2022-01-17 06:27:56 【问题描述】:

我正在编写一个脚本以从 CSV 文件中提取信息并将其粘贴到 .txt 文件中。在我将该文本提取后,通过一个应该删除任何重复项的脚本运行该 txt 文件。每一行数据都有很多信息。每行都应该有一个 IP 地址,并且可能是一个逻辑 ID。这是我的 csv 文件的示例。

"IslandView.pdf","","92","D1-B(2044556) Avigilon (ONVIF) 3.0C-H4A-D1-B Unknown Logical ID:558 "
"IslandView.pdf","","92","Detection: Disabled 2318 Avigilon (ONVIF) 3.0C-H4A-D1-B Unknown Logical ID:2318 "
"IslandView.pdf","","92","Detection: Disabled 1042 Avigilon (ONVIF) 3.0C-H4A-D1-B South Pool Deck Logical "
"IslandView.pdf","","92","D1-B(2086446) Avigilon (ONVIF) 3.0C-H4A-D1-B Unknown Logical ID:101 "
"IslandView.pdf","","92","Detection: Disabled 1016 Avigilon (ONVIF) 3.0C-H4A-D1-B Unknown Logical ID:1016 "
"IslandView.pdf","","92","Detection: Disabled 2013 Avigilon (ONVIF) 3.0C-H4A-D1-B Unknown Logical ID:2013 "
"IslandView.pdf","","92","Detection: Disabled 2007 Avigilon (ONVIF) 3.0C-H4A-D1-B Unknown Logical ID:2007 "
"IslandView.pdf","","92","Detection: Disabled 2092 Avigilon (ONVIF) 3.0C-H4A-D1-B Unknown Logical ID:2092 "
"IslandView.pdf","","92","H5A-D1(3022620) Avigilon (ONVIF) 3.0C-H4A-D1-B Unknown 192.168.60.244 00:18:"
"IslandView.pdf","","92","D1-B(2230651) Avigilon (ONVIF) 3.0C-H4A-D1-B Unknown Logical ID:411 "
"IslandView.pdf","","92","D1-B(2251578) Avigilon (ONVIF) 3.0C-H4A-D1-B Unknown Logical ID:512 "
"IslandView.pdf","","92","D1-B(2300912) Avigilon (ONVIF) 3.0C-H4A-D1-B Unknown Logical ID:451 "
"IslandView.pdf","","92","D1-B(2345291) Avigilon (ONVIF) 3.0C-H4A-D1-B Unknown Logical ID:410 "
"IslandView.pdf","","92","D1-B(2345293) Avigilon (ONVIF) 3.0C-H4A-D1-B Unknown Logical ID:149 "
"IslandView.pdf","","93","D1-B(2345337) Avigilon (ONVIF) 3.0C-H4A-D1-B Unknown Logical ID:161 "
"IslandView.pdf","","93","D1-B(2345304) Avigilon (ONVIF) 3.0C-H4A-D1-B Unknown Logical ID:357 "
"IslandView.pdf","","93","D1-B(2738897) Avigilon (ONVIF) 3.0C-H4A-D1-B Unknown Logical ID:193 "
"IslandView.pdf","","94","D1-B(2959462) Avigilon (ONVIF) 3.0C-H4A-D1-B Unknown Logical ID:380 "
"IslandView.pdf","","94","D1-B(2959515) Avigilon (ONVIF) 3.0C-H4A-D1-B Unknown Logical ID:721 "
"IslandView.pdf","","102","Detection: Unsupported 1001 Avigilon (ONVIF) 3.0C-H4A-D1-B valet key control Logical "
"IslandView.pdf","","102","Detection: Disabled 2168 Avigilon (ONVIF) 3.0C-H4A-D1-B Dockside Deli Logical ID:"
"IslandView.pdf","","140","Detection: Disabled 2318 Avigilon (ONVIF) 3.0C-H4A-D1-B Unknown 192.168.205.216 00:18:"
"IslandView.pdf","","140","Detection: Unsupported 3012 Avigilon (ONVIF) 3.0C-H4A-D1-B Unknown 192.168.200.241 00:18:"

正如我所说,如果您查看以“IslandView...”开头的每一行,则有一个逻辑 ID,其后面是一个 IP 地址。但是在某些行中没有列出 IP 地址,因此与其在下一行中使用下一个逻辑 ID 进入新行,而是将它们保持在所有行上,直到遇到 IP 地址并创建新行。

我面临的一个问题是,如果我在 else 语句中输入“\n”,它将在一行打印一行的逻辑 ID,然后在下一行打印 IP

我知道我的代码中的 \n 会出错,但我找不到修复方法

这是我的脚本中的代码,它从 csv 中提取数据并将其输入到 .txt:

import csv
import re
import sys

new_file = input("What is the camera model? **Use Exact Casing and Symbols** ")
file_path = "C:\\Users\\ADMIN-SURV\\Desktop\\data_pull\\filter_results\\"
end_path = file_path + new_file + ".txt"
print(end_path)

output_txt_file = open(end_path, 'x')
with open("C:\\Users\\ADMIN-SURV\\Desktop\\data_pull\\" + new_file + "_filter.csv", "r") as fid:
    # print(fid)
    input_file = csv.reader(fid)
    for row in input_file:
        if len(row) >= 4:
            if row[0] == 'File name':
                # skip the header row
                continue
            m = re.match(r'.*(.* [0-9]1,3\.[0-9]1,3\.[0-9]1,3\.[0-9]1,3)', row[3])
            if m:
                # print(m.group(1))
                output_txt_file.write(m.group(1).strip() + '\n')

            else:
                # find only integer string
                res = [int(i) for i in row[3].split() if i.isdigit()]
                # print(str(res))
                output_txt_file.write(str(res))
                # print("No IP or Logical ID")

sys.stdout.close()

以下是 .txt 文件的示例:

192.168.205.216
192.168.70.171
[]192.168.50.195
192.168.50.193
[][2065][1041][1010][2119][2093][2051][2110][2091]192.168.202.212
[1014][2169]192.168.201.219
[2135][2118][2090]192.168.202.215
192.168.202.213
[1046][2040][2076]192.168.202.216
[2039][3012][2030][2166][2145][3011][2109][3025]192.168.202.217
192.168.201.223
192.168.201.205
192.168.201.200
192.168.201.217
192.168.202.218
[3019][2043][2006][][1031][2035][2005][2041][2037][][][][][][][2170][][][][][]192.168.70.201
[]192.168.50.237
[][][2110][2109][2043][2090][2051][2166]192.168.11.201
[2036][2005][2063][2064][2065][2076][2066][2039][2041][2117][2037][2038][2045][2040][2091][2006][2118][2042][2119][2044][][][][][][][][][][2318][1042][][1016][2013][2007][2092]192.168.60.244
[][][][][][][][][][][1001][2168]192.168.205.216
192.168.200.241
192.168.90.203
192.168.206.201
192.168.50.193
192.168.200.239

您可以看到 [逻辑 ID] 没有移动到下一行。如果一行同时具有逻辑 id 和 IP,我想同时打印下一行。如果它只有一个或另一个,则打印其中的任何一个并移至下一行。

【问题讨论】:

【参考方案1】:

您可以在其中添加一点逻辑来检查它是否找到逻辑 ID 和/或 IP。

import csv
import re

new_file = input("What is the camera model? **Use Exact Casing and Symbols**")
file_path = "C:\\Users\\ADMIN-SURV\\Desktop\\data_pull\\filter_results\\"
end_path = file_path + new_file + ".txt"
print(end_path)

output_txt_file = open(end_path, 'w')
with open("C:\\Users\\ADMIN-SURV\\Desktop\\data_pull\\" + new_file + "_filter.csv", "r") as fid:
    print(fid)
    input_file = csv.reader(fid)
    for row in input_file:
        if len(row) >= 4:
            if row[0] == 'File name':
                # skip the header row
                continue

            # Check for Logical ID
            logicalID_match = re.match(r'.*Logical ID:(\d*)', row[3])
            
            # Check for IP
            ip_match = re.match(r'.*(.* [0-9]1,3\.[0-9]1,3\.[0-9]1,3\.[0-9]1,3)', row[3])
            
            if logicalID_match:
                logicalID_str = logicalID_match.group(1).strip()
            else:
                logicalID_str = ''
                    
            if ip_match:
                ip_str = ip_match.group(1).strip()
            else:
                ip_str = ''
                
            outputStr = ' '.join([logicalID_str,ip_str]).strip()
            
            if outputStr != '':
                output_txt_file.write(outputStr + '\n')

output_txt_file.close()            

【讨论】:

【参考方案2】:

看起来您提供的 CSV 中的每一行都有一个逻辑 ID 或一个 IP 地址,而从来没有两者都有(即包含 Logical ID: 的每一行都没有 IP 地址,反之亦然)。

由于没有一行同时具有逻辑 id 和 IP,因此您似乎可以在每次将值附加到文本文件时添加 \n

如果我以某种方式误解了您,请相应地编辑您的问题。谢谢!

【讨论】:

以上是关于找到逻辑 ID 和 IP 地址后,如何使此代码生成新行?的主要内容,如果未能解决你的问题,请参考以下文章

如何从给定子网中查找第一个和最后一个 IP 地址?

Java - 按名称DNS获取IP地址(?)

Win10怎么设置IP地址

如何通过邮件找到发件人的IP地址.

如何在源IP地址,目标IP地址,源端口,目标端口和l4协议上应用Toeplitz哈希,以便它可以生成哈希ID?

DB2通过db2top找到会话对应的ip地址