1 #! /usr/bin/env python
\r
4 ============LICENSE_START==========================================
\r
6 ===================================================================
\r
7 Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
\r
8 ===================================================================
\r
10 Unless otherwise specified, all software contained herein is licensed
\r
11 under the Apache License, Version 2.0 (the License);
\r
12 you may not use this software except in compliance with the License.
\r
13 You may obtain a copy of the License at
\r
15 http://www.apache.org/licenses/LICENSE-2.0
\r
17 Unless required by applicable law or agreed to in writing, software
\r
18 distributed under the License is distributed on an "AS IS" BASIS,
\r
19 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
20 See the License for the specific language governing permissions and
\r
21 limitations under the License.
\r
23 ECOMP is a trademark and service mark of AT&T Intellectual Property.
\r
24 ============LICENSE_END============================================
\r
27 #### Command line arguments
\r
29 parser = argparse.ArgumentParser(description="Convert delimited file from one delimiter to another; defaults to converting pipe-delimited to CSV.")
\r
30 parser.add_argument("--delim-input", action="store", dest="delim_in", default="|", required=False, help="Input file delimiter; pipe is default (|)", nargs='?', metavar="'|'")
\r
31 parser.add_argument("--delim-output", action="store", dest="delim_out", default=",", required=False, help="Output file delimiter; comma is default (,)", nargs='?', metavar="','")
\r
32 parser.add_argument("--remove-line-char", action="store_true", dest="remove_line_char", default=False, help="remove \\n and \\r characters in fields and replace with spaces")
\r
33 parser.add_argument("--quote-char", action="store", dest="quote_char", default='"', required=False, help="quote character; defaults to double quote (\")", nargs='?', metavar="\"")
\r
34 parser.add_argument("-i", "--input", action="store", dest="input", required=False, help="input file; if not specified, take from standard input.", nargs='?', metavar="file.csv")
\r
35 parser.add_argument("-o", "--output", action="store", dest="output", required=False, help="output file; if not specified, write to standard output", nargs='?', metavar="file.pipe")
\r
36 parser.add_argument("-v", "--verbose", action="store_true", dest="verbose", default=False, help="increase verbosity")
\r
37 args = parser.parse_args()
\r
39 # Conversion for logs begins here.
\r
45 file_reader = file.reader(open(args.input, 'rb'), delimiter=args.dlm_in, quotechar=args.quote_char)
\r
47 file_reader = file.reader(sys.stdin, delimiter=args.dlm_in, quotechar=args.quote_char)
\r
50 h_outfile = open(args.output, 'wb')
\r
52 h_outfile = sys.stdout
\r
54 for row in file_reader:
\r
55 row = args.dlm_out.join(row)
\r
56 if args.remove_line_char:
\r
57 row = row.replace('\n', ' ').replace('\r', ' ')
\r
58 h_outfile.write("%s\n" % (row))
\r