-
Notifications
You must be signed in to change notification settings - Fork 67
/
convert.py
61 lines (52 loc) · 1.71 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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Command line utility to convert .SPC files to .TXT
author: Rohan Isaac
"""
from __future__ import division, absolute_import, unicode_literals, print_function
import argparse
import os
import spc
def main():
desc = 'Converts *.spc binary files to text using the spc module'
parser = argparse.ArgumentParser(description=desc)
parser.add_argument('filefolder', nargs='+', help='Input *.spc files or directory')
fformat = parser.add_mutually_exclusive_group()
fformat.add_argument('-c', '--csv', help='Comma separated output file (.csv) [default]',
action='store_true')
fformat.add_argument('-t', '--txt', help='Tab separated output file (.txt)',
action='store_true')
args = parser.parse_args()
if args.txt:
exten = '.txt'
delim = '\t'
else:
# defaults
exten = '.csv'
delim = ','
flist = []
# add all files from input file name
for fn in args.filefolder:
ffn = os.path.abspath(fn)
# or directories
if os.path.isdir(ffn):
for f in os.listdir(ffn):
flist.append(os.path.join(ffn, f))
else:
flist.append(ffn)
# process files
for fpath in flist:
if fpath.lower().endswith('spc'):
foutp = fpath[:-4] + exten
try:
print(fpath, end=' ')
f = spc.File(fpath)
f.write_file(foutp, delimiter=delim)
print('Converted')
except:
print('Error processing %s' % fpath)
else:
print('%s not spc file, skipping' % fpath)
if __name__ == '__main__':
main()