新更新 CST 上午 11:30以下是我的完整代碼
所需的結果將是以下格式的 txt 檔案:
邏輯 ID:(&) 192.168.xx.xxx(如果兩者都有)
192.168.xx.xxx
邏輯標識:
192.168.xx.xxx
192.168.xx.xxx
邏輯標識:
邏輯標識:
192.168.xx.xxx
**最新代碼-> 如果存在,我想列印邏輯 ID,如果不存在,我希望它列印 IP 地址(到新表格)
代碼顯示我已經指定了一個 model.csv 來寫入一個 model.txt 并且必須逐個模型手動更改它。所以如果有一個解決方案,那也很棒
import csv
import re
import sys
sys.stdout = open("C:\\Users\\ADMIN-SURV\\Desktop\\data_pull\\2.0C-H4A-DC2 .txt", 'w')
with open('C:\\Users\\ADMIN-SURV\\Desktop\\data_pull\\2.0C-H4A-DC2_filter.csv') as fid:
inputfile = csv.reader(fid)
for row in inputfile:
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))
else:
print(row[3])
sys.stdout.close()
*** 錯誤決議行:模型未找到 H4SL-D1(2305854) Avigilon (ONVIF) 1.3C-H4SL-D1 Unknown 192.168.50.250 00:18:85:***
*** 錯誤決議行:模型未找到 H4SL-D1(2878617) Avigilon (ONVIF) 1.3C-H4SL-D1 Unknown 192.168.50.194 00:18:85:***
Avigilon (ONVIF) 1.3C-H4SL-D1 Unknown Logical ID:1026 *** error parsing line: model not found 不支持 SOUTH LV Avigilon (ONVIF) 1.3C-H4SL-D1 Unknown 192.168.206.250 05:**18:8 *
Avigilon (ONVIF) 1.3C-H4SL-D1 Unknown Logical ID:3027 *** error parsing line: model not found ELEVATOR GROUND FL Avigilon (ONVIF) 1.3C-H4SL-D1 Unknown 192.168.80.203 05:**18:88 *
當然,這只是回報的一小部分。
我想補充一點,CSV 檔案實際上是專門針對模型的,所以我實際上要做的就是從第一列中取出邏輯 ID,并將它們添加到定義模式下的串列中,如果沒有邏輯 ID附加,然后回傳給定的 IP 地址。
I searched for all instances of an item and saved the results as a CSV file. I am using python to try and grab specific information. I would love to add a photo but I am not allowed.
This is the error i am receiving
Traceback (most recent call last): File "C:\Users\ADMIN-SURV\PycharmProjects\pdf_scraping\test_file.,py", line 7, in print(column[3]) IndexError: list index out of range
This is the only code I have written:
import csv
inputfile = csv.reader(open('C:\\Users\\ADMIN-SURV\\Desktop\\data_pull\\Untitled.csv','r'))
for column in inputfile:
print(column[3])
When I get rid of the [3] in the last line and just leave
print(column)
It prints my entire CSV file in the console. All I want is specific information from each row and I can get that info by grabbing it from a specific column.
The CSV file data looks like this:
Search Results"
"Summary"
"Saved on","12/8/2021 1:57:21 PM"
"Searched for","Avigilon (ONVIF) 1.3C-H4SL-D1"
"In document","C:\Users\ADMIN-SURV\Desktop\data_pull\IslandView.pdf"
"Number of document(s) found","1"
"Number of instance(s) found","551"
"File name","Title","Page","Search Instance"
"IslandView.pdf","","5","Detection: Unsupported 2058 Avigilon (ONVIF) 1.3C-H4SL-D1 Unknown Logical ID:2058 192.168.202.206 "
"IslandView.pdf","","9","BAR POS 01 Avigilon (ONVIF) 1.3C-H4SL-D1 Unknown 192.168.70.214 00:18:85:"
"IslandView.pdf","","9","H4SL-D1(1866954) Avigilon (ONVIF) 1.3C-H4SL-D1 Unknown Logical ID:481 192.168.11.203 "
"IslandView.pdf","","9","H4SL-D1(1825930) Avigilon (ONVIF) 1.3C-H4SL-D1 Unknown Logical ID:20 192.168.16.203 "
"IslandView.pdf","","9","Detection: Unsupported 2048 Avigilon (ONVIF) 1.3C-H4SL-D1 Unknown 192.168.202.200 00:18:85:"
"IslandView.pdf","","9","H4SL-D1(1866877) Avigilon (ONVIF) 1.3C-H4SL-D1 Unknown Logical ID:92 192.168.15.205 "
"IslandView.pdf","","9","Detection: Unsupported 2074 Avigilon (ONVIF) 1.3C-H4SL-D1 Unknown Logical ID:2074 192.168.203.241 "
"IslandView.pdf","","9","Detection: Unsupported 2174 Avigilon (ONVIF) 1.3C-H4SL-D1 Unknown Logical ID:2174 192.168.201.232 "
"IslandView.pdf","","9","Detection: Unsupported 2161 Avigilon (ONVIF) 1.3C-H4SL-D1 Unknown Logical ID:2161 192.168.205.231 "
there are over 500 lines. You can see the column headers
"File name","Title","Page","Search Instance"
我想要的只是第一列中的型號和邏輯 ID 資訊。我想隔離然后制作一個有組織的串列,其中邏輯 ID 與哪個模型相關。
以防萬一,這是第 1 列的型號和邏輯 ID 示例
Avigilon (ONVIF) 1.3C-H4SL-D1 邏輯 ID:875
最終目標是創建一個作業表,其中列出了每個模型(這是一個模型的搜索結果),并且在該串列下方是與該模型關聯的所有邏輯 ID。
如果我能澄清或提供任何進一步的資訊,請告訴我。
謝謝!
uj5u.com熱心網友回復:
CSV 檔案的頂部包含少于 4 列的行。為了避免 IndexError 首先嘗試測驗行長度:
# "inputfile" is a CSV reader instance
for row in inputfile:
if len(row) >= 4:
print(row[3])
這是一個使用正則運算式來分解模型名稱的變體,在 IP 地址處停止:
import re
with open('example_data.csv') as fid:
inputfile = csv.reader(fid)
for row in inputfile:
if len(row) >= 4:
if row[0] == 'File name':
# skip the header row
continue
m = re.match(r'.*(Avigilon.* [0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})', row[3])
if m:
print(m.group(1))
else:
print(f'*** error parsing line: model not found {row[3]}***')
對于上述資料列印:
Avigilon (ONVIF) 1.3C-H4SL-D1 Unknown Logical ID:2058 192.168.202.206
Avigilon (ONVIF) 1.3C-H4SL-D1 Unknown 192.168.70.214
Avigilon (ONVIF) 1.3C-H4SL-D1 Unknown Logical ID:481 192.168.11.203
Avigilon (ONVIF) 1.3C-H4SL-D1 Unknown Logical ID:20 192.168.16.203
Avigilon (ONVIF) 1.3C-H4SL-D1 Unknown 192.168.202.200
Avigilon (ONVIF) 1.3C-H4SL-D1 Unknown Logical ID:92 192.168.15.205
Avigilon (ONVIF) 1.3C-H4SL-D1 Unknown Logical ID:2074 192.168.203.241
Avigilon (ONVIF) 1.3C-H4SL-D1 Unknown Logical ID:2174 192.168.201.232
Avigilon (ONVIF) 1.3C-H4SL-D1 Unknown Logical ID:2161 192.168.205.231
Avigilon (ONVIF) 1.3C-H4SL-D1 Unknown 192.168.50.246
要將輸出寫入文本檔案,請嘗試以下操作:
with open('logfile.txt', 'w') as fout:
with open('example_data.csv') as fid:
inputfile = csv.reader(fid)
for row in inputfile:
if len(row) >= 4:
if row[0] == 'File name':
# skip the header row
continue
m = re.match(r'.*(Avigilon.* [0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})', row[3])
if m:
# optional: delete this print line
print(m.group(1))
fout.write(f'{m.group(1)}\n')
else:
print(f'*** error parsing line: model not found {row[3]}***')
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/379608.html
下一篇:在iText7上合并PDF的問題
