diff --git a/cleanup-roster.py b/cleanup-roster.py index b80e95a..4af123a 100644 --- a/cleanup-roster.py +++ b/cleanup-roster.py @@ -42,8 +42,11 @@ def get_fields(self, fdno): else: return [self.fields[int(_)] for _ in fdno] -def write_csv(file, student_list, fields): - csvwriter = csv.writer(file) +def write_csv(file, student_list, fields, nl = None): + if nl is None: + csvwriter = csv.writer(file) + else: + csvwriter = csv.writer(file, lineterminator=nl) for s in student_list: csvwriter.writerow(s.get_fields(fields)) @@ -51,9 +54,10 @@ def write_csv(file, student_list, fields): parser.add_argument('infile', nargs='?', help='Input file.') parser.add_argument('-o', nargs='?', default='', const='', help='Output file.') parser.add_argument('-f', nargs='+', default='', help='List of field numbers.') +parser.add_argument("-a", action='store_true', default=False, help='Append to the output file.') args = parser.parse_args() -# print(args) +print(args) student_list = [] student = None @@ -102,7 +106,10 @@ def write_csv(file, student_list, fields): exit(1) if args.o != '': - with open(args.o, 'w', newline='') as csvfile: + flag = 'w' + if args.a: + flag = 'a+' + with open(args.o, flag, newline='') as csvfile: write_csv(csvfile, student_list, args.f) else: - write_csv(sys.stdout, student_list, args.f) + write_csv(sys.stdout, student_list, args.f, '\n') diff --git a/readme.MD b/readme.MD index 39b408f..31f6cc6 100644 --- a/readme.MD +++ b/readme.MD @@ -21,6 +21,8 @@ The `-o` option specifies an output file. python cleanup-roster.py downloaded.xls -o section1.csv ``` +The `-a` option specifies the append mode so the output file is not cleared. + The `-f` option specifies the fields to be included. `all` means all fields. Without `-f` option, only a few most useful fields are printed. @@ -30,3 +32,8 @@ python cleanup-roster.py downloaded.xls -f 1 2 python cleanup-roster.py downloaded.xls -f all ``` +In Powershell, the following command appends all students record to file all.csv. + +``` +dir *.xls | foreach { py .\cleanup-roster.py $_ -a -o all.csv } +```