I am working on a script to extract info from a CSV file and paste it into a .txt file. After I have that text extracted a run that txt file through a script that is supposed to remove any duplicates. Each row of data has a lot of information. Every row should have an IP Address and MAYBE a Logical ID. here is a sample of my csv file.
"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:"
As I stated If you look at each row preceded with "IslandView..." That there is a Logical ID and right behind that an IP Address. But in some rows there is no IP Address listed, so instead of breaking into a new line with the next logical ID on the following line, it's keeping them on all one line until it comes across an IP address and creates a new line.
A problem I Face with this is that if i put "\n" in my else statement, it will print one row's Logical ID on one line, then the IP on the next
I know it's going to be an error with my \n in my code but I can't find the fix
Here is my code from my script that pulls data from a csv and enters it into .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()
and here is an example of the .txt file:
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
AS you can see the [logical ID's] are not moving to the next line. If a line has both a logical id and IP, I want to print both then next line. If it only has one or the other, print whichever it has and move to next line.
CodePudding user response:
You can just add a little logic in there to check if it finds a Logical ID and/or 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()
CodePudding user response:
It looks like every line in the CSV you provided has either a Logical ID or an IP Address, never both (i.e. every line containing Logical ID:
doesn't have an IP address, and vice-versa).
Since no line has both a logical id and IP, it seems like you'd be able to add a \n
every time you append a value to the text file.
If I misunderstood you in some way, please edit your question accordingly. Thanks!