-
Notifications
You must be signed in to change notification settings - Fork 6
/
text_as_input.py
executable file
·38 lines (34 loc) · 1.43 KB
/
text_as_input.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
# -*- coding: utf-8 -*-
"""Created on Thu Jun 22 08:24:30 2017
@author: marnold
Creates command line arguments, checks for output filename,
parses input text file to python dictionary and returns
python dictionary and output filename
"""
import argparse
def txt2dictionary ():
# create parser and add arguments
parser = argparse.ArgumentParser()
parser.add_argument('file', type=argparse.FileType('r'), help = "must input a .txt file. see input.txt for example. Further documentation is in README.md")
parser.add_argument('-o', '--output', help = "output file, if not entered will default to 'out' + eventid + '.xml'")
args = parser.parse_args() # create arguments list
# read in file to python dictionary
lines = args.file.readlines()
data = {}
for line in lines:
if line[0] == "#" or line[0] == "" or line[0] == "\n":
pass
else:
temp = line.split(":::")
tmp = temp[1]
temp[1] = tmp[:-1]
data[temp[0]] = temp[1]
output_filename = ""
# Check for output file argument.
# If no output file argument, make an output filename.
if args.output:
output_filename = args.output
else: #
print ("No output file argument. Default filename will be generated...")
output_filename = "out_" + data['datasource'].lower() + data['eventid'] + '.xml' # set output filename
return data , output_filename