-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvert_em1.py
executable file
·90 lines (72 loc) · 2.95 KB
/
convert_em1.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
""" Look up items in a list from a dictionary. Results are printed to standard output. The user needs to provide a list in a file with one item per line. The dictionary to use must be also provided by the user in a two-column (tsv or csv) format."""
__author__ = "Ray Stefancsik"
__version__ = "2022-05-04"
# Modules to import
######################################################################
# Import command-line parsing module from the Python standard library.
######################################################################
import argparse
### Mandatory positional arguments
parser = argparse.ArgumentParser( description='Look up items from a dictionary, convert them, and print the results to standard output.' )
parser.add_argument( 'your_dictionary', help='Path to your dictionary file. FORMAT: Use two columns separated by tab (default) or comma (optional).' )
parser.add_argument( 'your_list', help='Path to your file with the list of items to convert using the dictionary. FORMAT: one item per line.' )
### Optional arguments:
parser.add_argument('-e', '--error_message', help='Specify an error message if a key is not in your specified dictionary table.', nargs='?', default='verbose', const='verbose' ) # if not specified by user then it defaults to given value
### Optional, but mutually exclusive user options:
group = parser.add_mutually_exclusive_group()
group.add_argument('-c', '--csv', help='Use comma as column separator.', action='store_true')
group.add_argument('-t', '--tab', help='Use tab as column separator.', action='store_true')
args = parser.parse_args()
######################################################################
# The dictionary to use must be provided by the user.
######################################################################
### Input file format:
if args.csv:
delimiter = ',' #csv file format
else:
delimiter = '\t' #tsv file format
# input filename
fname = args.your_dictionary
try:
fhand = open( fname )
except:
print('File cannot be opened:', fname)
exit()
# Make an empty dictionary:
d = dict()
for line in fhand:
line = line.rstrip()
columns = line.split(delimiter)
try:
k = columns[0]
v = columns[1]
except:
v = 'Check your dictionary file format.'
print( 'ERROR:', v)
exit()
if k not in d:
d[k] = v
# close file
fhand.close()
#################################################################
# Read in and convert data file.
#################################################################
fname = args.your_list
try:
fhand = open( fname )
except:
print('File cannot be opened:', fname)
exit()
for line in fhand:
words = line.rstrip()
try:
print(d[words])
except:
if args.error_message == 'verbose':
print( 'ERROR:', words ,'is not in your dictionary!!')
else:
print(args.error_message)
# close file
fhand.close()