0

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:

  1. Reads in a log file
  2. Creates a 2D array of timestamps from a log file
  3. Opens up a data file
  4. 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:

  1. Reads in a log file
  2. Creates a 2D array of timestamps from a log file
  3. Opens up a data file
  4. 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")
artemis
  • 6,857
  • 11
  • 46
  • 99
  • Possible duplicate of [Check if argparse optional argument is set or not](https://stackoverflow.com/questions/30487767/check-if-argparse-optional-argument-is-set-or-not) – SPYBUG96 Dec 03 '18 at 16:52
  • Also, to create a new file: https://stackoverflow.com/questions/48959098/how-to-create-a-new-text-file-using-python – SPYBUG96 Dec 03 '18 at 16:54
  • Thanks for your feedback, SPYBUG96. I think it is possible that the answer could be adopted as a fusion of your two comments, but I would reckon this is not a duplicate, as the specific application here is unique to both of the links. – artemis Dec 03 '18 at 16:59
  • Make `new-log` a `store_true` argument. It's default value will be `False`, and if given `True`. As written it requires an argument, with a default value of `None`. – hpaulj Dec 03 '18 at 17:31
  • When debugging `argparse` applications, it's a good idea to `print(args)` to see exactly what the parser has produced for various inputs. That way you won't be guessing when you try to use `args` in your code. – hpaulj Dec 03 '18 at 17:33
  • @hpaulj, it is funny you commented them. I JUST added that in (the store_true) argument based on: https://docs.python.org/3/howto/argparse.html – artemis Dec 03 '18 at 17:34

1 Answers1

0

After enough debugging I found the line that needed to change.
writer.writerow(row + [isBad]) needed to be writer.writerow([isBad])

artemis
  • 6,857
  • 11
  • 46
  • 99