forked from dkennard3/TSM_Accounting
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconvert_TSM.py
More file actions
executable file
·63 lines (54 loc) · 2.08 KB
/
convert_TSM.py
File metadata and controls
executable file
·63 lines (54 loc) · 2.08 KB
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
import sys
import re
import json
from datetime import datetime
from unidecode import unidecode
'''
1) convert unix Timestamp to datetime obj yyyy-mm-dd
2) remove colons and dashes, replace w/ empty '' string (except the date)
3) double quote '..' around itemString, itemName, otherplayer, player, and date
4) replace 'special' characters in otherPlayer with an A
re.sub(r'pattern', 'with_string', str)
'''
try:
export_file = sys.argv[1]
table_name = sys.argv[2]
except IndexError:
print('usage: python convert_TSM.py TSM_csv_export_file desired_table_name')
sys.exit(0)
else:
if re.findall(r'\d', table_name) or not table_name.strip():
print(f'\nInvalid name: {table_name} -- \
name cannot contain numbers nor be an empty string\n')
sys.exit(-1)
with open(export_file,'r') as f:
lines = f.readlines()
headers = lines[0].rstrip('\n').split(',')
with open('types.json', 'r') as f:
types = json.load(f)
headLine = f"create or replace table {table_name} (\n\t{headers}"
start = f'insert into {table_name} values ('
end = f');'
with open(f'{table_name}.txt', 'w') as f:
for line in lines:
tmp = []
line = line.replace(':', '')
line = line.replace('-', '')
line = line.replace('\'', '')
chunks = line.split(',')
for chunk in chunks:
# timestamp(date) of record
if re.match(r'\d{10}',chunk):
date = datetime.fromtimestamp(int(chunk))
date = str(date).split(' ')[0]
tmp.append('\''+date+'\'')
# any number (int or float) that is not a timestamp
elif re.match(r'\d{1,4}',chunk):
if headers[chunks.index(chunk)] in ('price','amount'):
tmp.append(str(round((float(chunk)/10000),2)))
else:
tmp.append(chunk)
# A Player's name, itemString, itemName, or source
elif re.match(r'[((\D\s?)\+)|(i\:\d\+)]',chunk):
tmp.append('\''+unidecode(chunk).rstrip('\n')+'\'')
f.write(start+','.join(tmp)+end+'\n')