-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogparse.py
More file actions
34 lines (27 loc) · 1.02 KB
/
Copy pathlogparse.py
File metadata and controls
34 lines (27 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import sys
import csv
def parse_line(line):
line = line.strip()
if not line:
return None
try:
url,username,password = line.rsplit(":",2)
return url,username,password
except ValueError:
return None
def lexer_file_to_csv(input_path, output_path):
with open(input_path, "r", encoding="utf-8", errors="ignore") as infile, open(output_path, "w", newline="", encoding="utf-8") as outfile:
writer = csv.writer(outfile)
writer.writerow(["url", "username", "password"])
for line_number, line in enumerate(infile, start=1):
tokens = parse_line(line)
if tokens:
writer.writerow(tokens)
else:
print(f"Skipping malformed line {line_number}: {line.strip()}")
if __name__ == "__main__":
input_file = str(sys.argv[1])
output_file = str(sys.argv[2])
if len(sys.argv) <3:
print("Usage: python3 logparse.py <inputfile.txt> <outputfile.csv>")
lexer_file_to_csv(input_file, output_file)