-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsv_writer.py
68 lines (52 loc) · 2.07 KB
/
csv_writer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import csv
from datetime import date
#import pprint
class CsvWriter:
'''
A class that creates a CSV file. Instantiating this class will generate the CSV file.
Also includes file management methods (archiving, diffs, etc.)
:param str filename: the CSV filename (extension optional)
:param list(dict(str, str)) data: a list of dictionaries that will be
printed to a single CSV row
:keyword bool datestamp: flag that appends a datestamp to the filename
(default: False)
:keyword list(str) headers: a list of keys that determines the order in
which the columns are printed (default: order of the keys of the first
entry in data)
'''
def __init__(self, filename, data, **opts):
self.base_filename = filename
self.files = []
if opts.get('datestamp', False):
filename += "_" + date.today().strftime('%F')
if not filename.endswith('.csv'):
filename += '.csv'
self.filename = filename
#pp = pprint.PrettyPrinter()
with open(filename, 'w') as csvfile:
writer = csv.writer(csvfile)
headers = opts.get('headers', data[0].keys())
# Anki doesn't have an option to ignore headers, so we can't really
# write them:
#writer.writerow(headers)
for datum in data:
writer.writerow([datum[key] if key in datum else '' for key in headers])
print(f'Wrote to {filename}')
def diff(self):
import subprocess
if len(self.files) == 0:
self.glob()
self.glob()
if len(self.files) > 1:
subprocess.run(['diff', self.files[-2], self.files[-1]])
def glob(self):
import glob
self.files = glob.glob(f'{self.base_filename}*.csv')
def archive(self):
import subprocess
if len(self.files) == 0:
self.glob()
if len(self.files) > 1:
subprocess.run(['mv', f'{self.files[-2]}', 'archived/'])
else:
print('No other files to archive')