Initial commit - iis2csv.py script
This commit is contained in:
49
iis2csv.py
Normal file
49
iis2csv.py
Normal file
@ -0,0 +1,49 @@
|
||||
# Standard imports
|
||||
import csv
|
||||
import sys
|
||||
import os
|
||||
|
||||
def convert_iis_log_to_csv(iis_log_path):
|
||||
try:
|
||||
with open(iis_log_path, 'r', encoding='utf-8') as log_file:
|
||||
lines = log_file.readlines()
|
||||
except FileNotFoundError: # Oop, file not found
|
||||
print(f"Error: File not found: {iis_log_path}")
|
||||
sys.exit(1)
|
||||
|
||||
# Set up some arrays
|
||||
field_names = []
|
||||
data_lines = []
|
||||
|
||||
# Read the file
|
||||
for line in lines:
|
||||
line = line.strip()
|
||||
if line.startswith("#Fields:"): # Look for the Fields line in the IIS log and use that to make column headers
|
||||
field_names = line[len("#Fields:"):].strip().split()
|
||||
elif line.startswith("#") or not line:
|
||||
continue # Skip any blank or commented lines
|
||||
else:
|
||||
data_lines.append(line.split())
|
||||
|
||||
if not field_names: # If no Fields line, it'll error, so you really want the Fields line in the file
|
||||
print("Error: No '#Fields:' line found in the log file.")
|
||||
sys.exit(1)
|
||||
|
||||
base_name = os.path.splitext(os.path.basename(iis_log_path))[0]
|
||||
output_csv_path = f"{base_name}.csv" # The CSV will be the same file name as the log file
|
||||
|
||||
# Create the CSV
|
||||
with open(output_csv_path, 'w', newline='', encoding='utf-8') as csv_file:
|
||||
writer = csv.writer(csv_file)
|
||||
writer.writerow(field_names)
|
||||
writer.writerows(data_lines)
|
||||
|
||||
print(f"Conversion complete. CSV written to: {output_csv_path}")
|
||||
|
||||
if __name__ == "__main__":
|
||||
if len(sys.argv) != 2:
|
||||
print("Usage: python convert_iis_log.py <iis_log_file>") # The most obvious help statement ever
|
||||
sys.exit(1)
|
||||
|
||||
iis_log_path = sys.argv[1]
|
||||
convert_iis_log_to_csv(iis_log_path)
|
Reference in New Issue
Block a user