Skip to content

Commit

Permalink
add -a option. change newline for stdout.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jerry Shi committed Dec 24, 2021
1 parent d299b60 commit 011c999
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
17 changes: 12 additions & 5 deletions cleanup-roster.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,22 @@ 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))

parser = argparse.ArgumentParser(description='Clean up downloaded roster from HuskyCT')
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
Expand Down Expand Up @@ -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')
7 changes: 7 additions & 0 deletions readme.MD
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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 }
```

0 comments on commit 011c999

Please sign in to comment.