-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvert.py
63 lines (51 loc) · 1.53 KB
/
convert.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
# author: Paul Galatic
#
# Program to assist in data conversion.
# STD LIB
import csv
import sys
import argparse
# LOCAL LIB
from extern import *
# CONSTANTS
TIME_FORMAT = '%H:%M:%S'
def parse_args():
'''Parses arguments'''
ap = argparse.ArgumentParser()
ap.add_argument('fname', type=str,
help='The file to convert into the new format. THE FULL PATH IS REQUIRED.')
return ap.parse_args()
def main():
'''Driver program'''
args = parse_args()
log('Starting...')
with open(args.fname, 'r', newline='', encoding='utf-8') as readin:
rdr = csv.reader(readin)
newrows = []
counter = 0
for row in rdr:
if row[-1] == 'id':
# The header is here, so we've already converted this file.
log('This file has already been converted.')
sys.exit(1)
newrow = {
'index': counter,
'text': row[1],
'timestamp': row[0],
'fav_count': None,
'ret_count': None,
'username': None,
'at_tag': None,
'id': None
}
newrows.append(newrow)
counter += 1
with open(args.fname, 'w', newline='', encoding='utf-8') as writeout:
wtr = csv.DictWriter(writeout, fieldnames=GATHER_FIELDNAMES)
wtr.writeheader()
for row in newrows:
wtr.writerow(row)
log('...finished.')
return 0
if __name__ == '__main__':
main()