I was just last week moved to the R&D department at work and as part of my fist task I've got to modify this Python code, but I've got no idea how. Basically, this script takes csv files coming in from the field and checks to see if all of the rows have the appropiate number of datum. If they don't this script appends datum, but I want to change it to delete the offending row.
I can do this easily enough in other languages, but I'm not framilar enough with Python to know how to do it within the confines of this code. Does anyone have any ideas? I only started with Python 8 weeks ago. I come from C++ and SQL so 'thinking in Python' is still new to me.
from asyncio import SubprocessTransport
import csv
import fileinput
import glob
import os
import numpy as np
import pandas as pd
from sqlalchemy import create_engine
# Declaring variables
# path to files -- parent folder is path
path = 'C:/Users/klucas/Desktop/zipped/Event Files'
key_path = 'C:/Users/klucas/Desktop/zipped'
newFile = path + '/EventFile.csv'
#machine ID minus date -- PS and TD header
machineID = '14:14:08','21012','223','0','1098','0','031','810','12','01','092','048','0008','02'
# Event File Data and Date
EFID = 'EF', '08/24/2021'
eventHeader = 'Event','Date/Time'
# Trend Data and Date
TDID = 'TD', '08/24/2021'
trendHeader = 'DATE/TIME','G120010','M129000','G110100','M119030','G112070','G112080','G111030','G127020','G127030','G120020','G120030','G121020','G111040','G112010','P102000','G112020','G112040','G112090','G110050','G110060','G110070','T111100'
TDmachineID = TDID + machineID
TD_files = glob.glob(os.path.join(path,"*.csv"), recursive=True)
##Trend Data Machine ID
EFmachineID = EFID + machineID
#sorting lists of all files csv in path
EF_files = glob.glob(os.path.join(path,"*.csv"), recursive=True) #- glob.glob(os.path.join(path,"Event Translator.csv"), recursive=True)
Key_file = glob.glob(os.path.join(key_path,"*.csv"), recursive=True)
data = []
Data = []
finalData = []
#Comments to right
for k in Key_file:
with open(k,newline='') as h:
q = csv.reader(h)
keyData = [line for line in q] #sets key data to a list of the Event Translator Data
for f in EF_files: #looping through trend files
with open(f,newline='') as g: #opening file as read
r = csv.reader(g) #declaring read variable for list
rawdata = [line for line in r] #setting list to all data in file
for j in rawdata:
del j[0]
del j[2]
del j[2]
del j[2]
del j[2]
del j[2]
del j[2]
del j[2]
del j[3]
del j[3]
del j[3]
for x in range(len(rawdata)):
if rawdata[x][2] != rawdata[x-1][2] and x != 0:
information = (rawdata[x][0],rawdata[x][1],rawdata[x][2])
data.append(information)
for y in range(len(data)):
if data[y][2] != data[y-1][2]:
info = (data[y][0],data[y][1],data[y][2])
Data.append(info)
for z in range(len(Data)):
j = len(keyData) - 1
total = int(float(Data[z][2]))
if total == 0:
continue
else:
while j > 0 :
keyValue = int(float(keyData[j][1]))
remainder = total - keyValue
if remainder == 0:
tup = (Data[z][0],Data[z][1],keyData[j][2])
finalData.append(tup)
break
elif remainder > 0:
tup = (Data[z][0],Data[z][1],keyData[j][2])
finalData.append(tup)
total = remainder
j = len(keyData) - 1
continue
else: j -= 1
#print(finalData)
finalData.insert(0,eventHeader) #updating first line to correct trend header
finalData.insert(0,EFmachineID) #inserting machine id line at top of page
with open(newFile,'w',newline='') as m:
w = csv.writer(m)
w.writerows(finalData)
for f in TD_files: #looping through trend files
with open(f,newline='') as g: #opening file as read
r = csv.reader(g) #declaring read variable for list
data = [line for line in r] #setting list to all data in file
# for j in data:
# del j[0]
# del j[9]
data[0] = trendHeader #updating first line to correct trend header
data.insert(0,TDmachineID) #inserting machine id line at top of page
with open(f,'w',newline='') as g: #opening file as write
w = csv.writer(g) #declaring write variable
w.writerows(data) #writing data list into new sheet
print('Complete!')