I am new to Python and am hoping to get some assistance with translating my pseudocode into real code. I currently have a python script which accomplishes the following:
- Reads in a log file
- Creates a 2D array of timestamps from a log file
- Opens up a data file
- Compares timestamps (first column), and if the value is IN or between the values in the 2D array, mark a "1" in the last column of the data file. If not, mark a 0.
What I am aiming to do is configure a command line argument that is optional, but if given, will instead write the 0 or 1 to an output file called "output.csv", instead of to the data file.
Currently, output.csv gets created, but not just with the 1 or 0 classification. It essentially rewrites the entire data.csv (that is read in) file into output.csv (that is supposed to be written to)
Would somebody be able toI am new to Python and am hoping to get some assistance with translating my pseudocode into real code. I currently have a python script which accomplishes the following:
- Reads in a log file
- Creates a 2D array of timestamps from a log file
- Opens up a data file
- Compares timestamps (first column), and if the value is IN or between the values in the 2D array, mark a "1" in the last column of the data file. If not, mark a 0.
What I am aiming to do is configure a command line argument that is optional, but if given, will instead write the 0 or 1 to an output file called "output.csv", instead of to the data file.
How can I edit my current script to not rewrite the entire data.csv file, but only the isBad
0 or 1 indicator?
import re
import csv
import codecs
import argparse
#Configuring arguments and variables
################################################
parser = argparse.ArgumentParser(description='This script is used to automatically classify the workbench operations. If the operation was performed by a human, it will be marked appropriately. If it was done by a machine, it will be marked appropriately.')
parser.add_argument('-d', '--data', required=True, help='The data.csv produced by the workbench')
parser.add_argument('-r', '--log', required=True, help='The log file used to appropriately label the data')
parser.add_argument('-n', '--new', required=False, help='Flag to create output.csv of markings instead of marking data.csv', action="store_true")
args = parser.parse_args()
if (args.new):
print("You selected to create a new log")
print('data.csv:', args.data)
print('log:', args.log)
filepath = args.log
csv_filepath = args.data
tempStart = ''
tempEnd = ''
################################################
print(" ")
print("Starting Script")
print(" ")
#open the log
with open(filepath) as myFile:
#read the log
all_logs = myFile.read()
myFile.close()
#Create regular expressions
starting_regex = re.compile(r'\[(\d+)\s+s\]\s+Initializing\s+Workbench')
ending_regex = re.compile(r'\[(\d+)\s+s\]\s+Log\s+File\s+Completed.\s+Stopping!')
#Create arrays of start and end times
start_times = list(map(int, starting_regex.findall(all_logs)))
end_times = list(map(int, ending_regex.findall(all_logs)))
#Create 2d Array
timeArray = list(map(list, zip(start_times, end_times)))
#Print 2d Array
print(timeArray)
print(" ")
print("Completed timeArray construction")
print(" ")
#Open the csv file as a reader
with open(csv_filepath, 'rb') as csvfile:
reader = csv.reader(codecs.iterdecode(csvfile, 'utf-8'))
input_rows = [row for row in reader]
#Open the csv file as a writer
with open('output.csv', 'w') as outputfile:
writer = csv.writer(outputfile)
# loop through the rows, set the currVal to the value of the first column (timestamp)
for row in input_rows:
currVal = int(row[0])
isBad = '0'
for interval in timeArray:
if interval[0] <= currVal <= interval[1]:
isBad = '1'
break
writer.writerow(row + [isBad])
print("Script completed")