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