I have a folder with multiple files, each with a varying number of columns in each file. I want to go through the directory, open each file and loop through each line, writing the line to a new CSV file based on the number of columns in that line. I want to end up with a single big CSV for all lines with 14 columns, another big CSV for all lines with 18 columns, and the last CSV with all the other columns.
Here's what I have so far.
import pandas as pd
import glob
import os
import csv
path = r'C:\Users\Vladimir\Documents\projects\ETLassig\W3SVC2'
all_files = glob.glob(os.path.join(path, "*.log"))
for file in all_files:
for line in file:
if len(line.split()) == 14:
with open('c14.csv', 'wb') as csvfile:
csvwriter = csv.writer(csvfile, delimiter=' ')
csvwriter.writerow([line])
elif len(line.split()) == 18:
with open('c14.csv', 'wb') as csvfile:
csvwriter = csv.writer(csvfile, delimiter=' ')
csvwriter.writerow([line])
#open 18.csv
else:
with open('misc.csv', 'wb') as csvfile:
csvwriter = csv.writer(csvfile, delimiter=' ')
csvwriter.writerow([line])
print(c14.csv)
Can anyone offer any feedback on how to approach this?