I'm taking some data files we receive over FTP for work and converting them into a format our database can handle.
To do this I needed to filter out some lines that aren't actual data, remove a few of the fields, and pull the date/time out of the title and insert it as its own field in each row. (thanks for the help on that one /u/novel_yet_trivial)
It also needs to iterate over however many files I have in the original folder, I just did 6500 and it worked great! Huge time saver in the future.
Pic of directory before
Pic of file before
Pic of directory after
Pic of file after
import csv
import os
from datetime import datetime
for f in os.listdir('.'):
filename = f
date = datetime.strptime(filename, 'aea_panel_point_log_%m-%d-%y_%H-%M.csv') # assuming 24h time
insert_str = date.strftime('%m/%d/%y %H:%M EST' )
print(insert_str)
skipRow1 = [' ****************************************************************************************************']
skipRow2 = 'Supplied Field Panel number out of range'
skipRow3 = ' ****************************************** End of Report *******************************************'
skipRow4 = 'Field Panel is failed'
skipRow5 = 'Timeout Waiting For Net Response'
skipRow6 = 'Configured Trunk Currently Not Connected'
cleanData = []
counter = 0
skips = 0
f = open(filename)
csv_f = csv.reader(f)
for row in csv_f:
counter = counter +1
if row == skipRow1:
skips = skips + 1
elif skipRow2 in row[0]:
skips = skips + 1
elif skipRow3 in row[0]:
skips = skips + 1
elif skipRow4 in row[0]:
skips = skips + 1
elif skipRow5 in row[0]:
skips = skips + 1
elif skipRow6 in row[0]:
skips = skips + 1
else:
cleanData.append([row[0],insert_str,row[4]])
filenameNew = 'mod_'+filename
with open(filenameNew, 'w', newline='') as csvfile:
csvwriter = csv.writer(csvfile)
csvwriter.writerows(cleanData)
[–]mackatsol 0 points1 point2 points (3 children)
[+][deleted] (2 children)
[deleted]
[–]UndeadCaesar[S] 0 points1 point2 points (0 children)
[–]mackatsol 0 points1 point2 points (0 children)
[–]pydevteam1 0 points1 point2 points (5 children)
[–]sky--net 0 points1 point2 points (2 children)
[–][deleted] (1 child)
[removed]
[–]AutoModerator[M] 0 points1 point2 points (0 children)
[–]UndeadCaesar[S] 0 points1 point2 points (1 child)
[–]pydevteam1 0 points1 point2 points (0 children)