-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathtln_parse.py
More file actions
executable file
·63 lines (51 loc) · 1.51 KB
/
tln_parse.py
File metadata and controls
executable file
·63 lines (51 loc) · 1.51 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
#!/usr/bin/env python3
#
# Description: parse.exe wasn't cutting it for me and wasn't doing much, so
# I figured I'd replace it with a python script
#
# Author: Jim Clausing
# Date: 2021-01-07
#
import sys
import os
import argparse
from datetime import *
from time import *
import contextlib
import codecs
import chardet
__version_info__ = (0, 1, 1)
__version__ = ".".join(map(str, __version_info__))
@contextlib.contextmanager
def smart_open(filename=None):
# KAPE made the TLN file a UTF-16-LE file, this detects that and sets encoding accordingly
if filename and filename != "-":
fh = open(filename, "r")
rawdata = open(filename, "rb").read()
result = chardet.detect(rawdata)
charenc = result["encoding"]
fh.close()
fh = open(filename, "r", encoding=charenc)
else:
fh = sys.stdin
try:
yield fh
finally:
if fh is not sys.stdin:
fh.close()
def parse_line(line):
line = line.rstrip()
i = line.find(",")
if i <= 0:
l = line.split("|")
l[0] = datetime.utcfromtimestamp(int(l[0])).strftime("%Y-%m-%d %H:%M:%S")
print(",".join(l))
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Parse/Transform TLN files")
parser.add_argument("files", metavar="FILE", nargs="*", default="-", help="TLN file")
args = parser.parse_args()
for path in args.files:
with smart_open(path) as f:
for line in f:
parse_line(line)
sys.exit(0)