Conversion Script to turn output.txt into csv
crxssrazr93 opened this issue · 0 comments
crxssrazr93 commented
If you create a folder called csv, and place the --output file as a .txt and run the script, it will look at all .txt files in that folder and convert them into a usable .csv.
I tried for hours but I wasn't able to integrate the conversion logic into the project for a seamless experience as I am not very good at python.
Hope this helps someone.
Script:
import argparse
import csv
import os
import ast
def process_input(input_file):
output_file = os.path.splitext(input_file)[0] + '.csv'
with open(input_file, 'r') as f:
lines = f.readlines()
headers = ['Method', 'Code', 'Server', 'Port', 'Host', 'Location']
data = []
for line in lines:
line = line.strip()
if line:
try:
line_data = ast.literal_eval(line)
method = line_data.get('method', '')
code = str(line_data.get('status_code', ''))
server = line_data.get('server', '')
port = line_data.get('port', '')
host = line_data.get('host', '')
location = line_data.get('location', '')
if location and '->' in location:
host, location = location.split(' -> ', 1)
data.append([method, code, server, port, host, location])
except ValueError:
print(f"Skipping invalid line: {line}")
with open(output_file, 'w', newline='') as f:
writer = csv.writer(f)
writer.writerow(headers)
writer.writerows(data)
def main():
parser = argparse.ArgumentParser(description='Process .txt files to .csv')
parser.add_argument('--input-dir', type=str, help='Input directory path', default='.')
args = parser.parse_args()
input_dir = args.input_dir
for filename in os.listdir(input_dir):
if filename.endswith('.txt'):
input_file = os.path.join(input_dir, filename)
process_input(input_file)
if __name__ == '__main__':
main()