Python参数写入不同的文件

时间:2018-12-03 15:35:47

标签: python arguments parameter-passing argparse

我是Python的新手,希望能将伪代码转换为真实代码获得帮助。我目前有一个python脚本,可以完成以下任务:

  1. 读取日志文件
  2. 从日志文件创建时间戳的二维数组
  3. 打开数据文件
  4. 比较时间戳记(第一列),如果值是IN或2D数组中的值之间,请在数据文件的最后一列中标记为“ 1”。如果不是,则标记为0。

我的目标是配置一个可选的命令行参数,但是如果给定的话,它将0或1写入名为“ output.csv”的输出文件,而不是数据文件。

当前,创建了output.csv,但不仅具有1或0分类。实际上,它将整个data.csv(读入)文件重写为output.csv(应该写入)

有人可以使用Python吗?希望将我的伪代码转换为真实代码时能得到一些帮助。我目前有一个python脚本,可以完成以下任务:

  1. 读取日志文件
  2. 从日志文件创建时间戳的二维数组
  3. 打开数据文件
  4. 比较时间戳记(第一列),如果值是IN或2D数组中的值之间,请在数据文件的最后一列中标记为“ 1”。如果不是,则标记为0。

我的目标是配置一个可选的命令行参数,但是如果给定的话,它将0或1写入名为“ output.csv”的输出文件,而不是数据文件。

如何编辑当前脚本以不重写整个data.csv文件,而仅重写isBad 0或1指示器?

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")

1 个答案:

答案 0 :(得分:0)

经过足够的调试,我发现需要更改的行。
writer.writerow(row + [isBad])必须为writer.writerow([isBad])