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