I have a bunch of CSV files with the name (modelnumber)_filter. The user is asked which model they are searching for so
example user input : "1.3C-H4SL-D1"
So then the file path will be loaded:
"C:\Users\ADMIN-SURV\Desktop\data_pull\1.3C-H4SL-D1_filter.csv"
EX CSV
"Search Results"
"Summary"
"Saved on","12/10/2021 1:36:26 PM"
"Searched for","Avigilon (ONVIF) 2.0C-H5A-D1"
"In document","C:\Users\ADMIN-SURV\Desktop\data_pull\my.pdf"
"Number of document(s) found","1"
"Number of instance(s) found","18"
"File name","Title","Page","Search Instance"
"IslandView.pdf","","33","H5A-D1(3022506) Avigilon (ONVIF) 2.0C-H5A-D1 Unknown Logical ID:692 192.168.50.189 "
"IslandView.pdf","","55","H5A-D1(3022509) Avigilon (ONVIF) 2.0C-H5A-D1 Unknown Logical ID:657 192.168.60.243 "
"IslandView.pdf","","55","H5A-D1(3022547) Avigilon (ONVIF) 2.0C-H5A-D1 Unknown Logical ID:469 192.168.60.248 "
"IslandView.pdf","","55","H5A-D1(3022533) Avigilon (ONVIF) 2.0C-H5A-D1 Unknown Logical ID:700 192.168.50.190 "
"IslandView.pdf","","87","104 RIGHT ENTRY Avigilon (ONVIF) 2.0C-H5A-D1 Unknown 192.168.90.212 00:18:85:"
"IslandView.pdf","","87","101 LEFT ENTRY Avigilon (ONVIF) 2.0C-H5A-D1 192.168.50.243 00:18:85:2E:"
"IslandView.pdf","","87","H5A-D1(3022627) Avigilon (ONVIF) 2.0C-H5A-D1 Unknown Logical ID:55 192.168.60.249 "
"IslandView.pdf","","88","H5A-D1(3669534) Avigilon (ONVIF) 2.0C-H5A-D1 Unknown Logical ID:463 192.168.18.202 "
"IslandView.pdf","","94","103 LEFT ENTRY Avigilon (ONVIF) 2.0C-H5A-D1 Unknown 192.168.60.245 00:18:85:"
"IslandView.pdf","","146","104 RIGHT ENTRY Avigilon (ONVIF) 2.0C-H5A-D1 Unknown 192.168.90.212 00:18:85:"
"IslandView.pdf","","201","H5A-D1(3022509) Avigilon (ONVIF) 2.0C-H5A-D1 Unknown 192.168.60.243 00:18:85:"
"IslandView.pdf","","201","H5A-D1(3022506) Avigilon (ONVIF) 2.0C-H5A-D1 Unknown 192.168.50.189 00:18:85:"
"IslandView.pdf","","201","H5A-D1(3022533) Avigilon (ONVIF) 2.0C-H5A-D1 Unknown 192.168.50.190 00:18:85:"
"IslandView.pdf","","201","H5A-D1(3022547) Avigilon (ONVIF) 2.0C-H5A-D1 Unknown 192.168.60.248 00:18:85:"
"IslandView.pdf","","201","101 LEFT ENTRY Avigilon (ONVIF) 2.0C-H5A-D1 Unknown 192.168.50.243 00:18:85:"
"IslandView.pdf","","201","H5A-D1(3022622) Avigilon (ONVIF) 2.0C-H5A-D1 Unknown 192.168.60.245 00:18:85:"
"IslandView.pdf","","202","H5A-D1(3022627) Avigilon (ONVIF) 2.0C-H5A-D1 Unknown 192.168.60.249 00:18:85:"
"IslandView.pdf","","203","H5A-D1(3669534) Avigilon (ONVIF) 2.0C-H5A-D1 Unknown 192.168.18.202 00:18:85:"
Then a new file will also be created with that input as:
"C:\Users\ADMIN-SURV\Desktop\data_pull\1.3C-H4SL-D1.txt"
Here is my code.
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)
sys.stdout = open(end_path, 'x')
with open("C:\\Users\\ADMIN-SURV\\Desktop\\data_pull\\" new_file "_filter.csv") 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])
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()
As of now when I run this code I get the following error
Traceback (most recent call last): File "C:\Users\ADMIN-SURV\PycharmProjects\pdf_scraping\test_file.,py", line 28, in with open("C:\Users\ADMIN-SURV\Desktop\data_pull\" new_file "_filter.csv") as fid: FileNotFoundError: [Errno 2] No such file or directory: 'C:\Users\ADMIN-SURV\Desktop\data_pull\1.3C-H4SL-D1_filter.csv'
The CSV file already exists and should be found with user input. The .TXT file should be created with user input.
CodePudding user response:
The reason you get
<_io.TextIOWrapper name='C:\Users\maxim\PycharmProjects\FB_Books\data_pull\1.3C-H4SL-D1_filter.csv' mode='r' encoding='cp1252'>
is because of sys.stdout = open(end_path, 'x')
.
open(end_path, 'x')
returns
<_io.TextIOWrapper name='C:\Users\maxim\PycharmProjects\FB_Books\data_pull\1.3C-H4SL-D1_filter.csv' mode='r' encoding='cp1252'>
So we're just going to clean up the logic a bit. We'll open the new file with 'x'
parameter (meaning it'll create the file if not created, and write. However, if it's already created, it won't allow to open and overwrite it)
Then using your logic, we'll just write the ip address to that file.
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") 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])
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).strip())
output_txt_file.write(m.group(1).strip() '\n')
else:
print(row[3])
output_txt_file.close()
CodePudding user response:
Ran it with Python 3.8.0, the result was a file in ../filter_results with no extension. I changed it to be .txt and this is its content:
<_io.TextIOWrapper name='C:\Users\maxim\PycharmProjects\FB_Books\data_pull\1.3C-H4SL-D1_filter.csv' mode='r' encoding='cp1252'>