-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathtln_parse.py
More file actions
executable file
·64 lines (52 loc) · 1.65 KB
/
tln_parse.py
File metadata and controls
executable file
·64 lines (52 loc) · 1.65 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
64
#!/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: 2026-03-17
#
import sys
import os
import argparse
from datetime import *
from time import *
import contextlib
import codecs
import chardet
__version_info__ = (0, 2, 0)
__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 != "-":
with open(filename, "rb") as raw_fh:
rawdata = raw_fh.read()
result = chardet.detect(rawdata)
charenc = result.get("encoding") or "utf-8"
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")
parser.add_argument('-V', '--version', action='version', help='print version number',
version='%(prog)s v' + __version__)
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)