sparse_csv.py
Go to the documentation of this file.
1 #!/usr/bin/python
2 #
3 # Software License Agreement (BSD License)
4 #
5 # Copyright (c) 2008, Willow Garage, Inc.
6 # All rights reserved.
7 #
8 # Redistribution and use in source and binary forms, with or without
9 # modification, are permitted provided that the following conditions
10 # are met:
11 #
12 # * Redistributions of source code must retain the above copyright
13 # notice, this list of conditions and the following disclaimer.
14 # * Redistributions in binary form must reproduce the above
15 # copyright notice, this list of conditions and the following
16 # disclaimer in the documentation and/or other materials provided
17 # with the distribution.
18 # * Neither the name of the Willow Garage nor the names of its
19 # contributors may be used to endorse or promote products derived
20 # from this software without specific prior written permission.
21 #
22 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 # POSSIBILITY OF SUCH DAMAGE.
34 
35 # Author: Kevin Watts
36 
37 
38 # Make any csv into sparse csv
39 
40 
41 PKG = 'diagnostic_analysis'
42 import roslib
43 roslib.load_manifest(PKG)
44 
45 import csv, os, sys
46 from optparse import OptionParser
47 
48 from diagnostic_analysis.sparse import make_sparse_skip, make_sparse_length
49 
50 if __name__=='__main__':
51  # Allow user to set output directory
52  parser = OptionParser()
53  parser.add_option("-l", "--length", dest="length",
54  help="Set length of output CSV", metavar="LEN",
55  default=None, action="store")
56  parser.add_option("-s", "--skip", dest="skip",
57  help="Skip every nth row. If length set, will ignore this value.",
58  metavar="SKIP", default=10, action="store")
59  parser.add_option("-m", "--max", dest="max",
60  help="Make largest possible file for Open Office (65k lines). If selected, other options ignored.",
61  metavar="MAX", default=False, action="store_true")
62 
63  options, args = parser.parse_args()
64 
65  # Get CSV file
66  if len(args) < 1:
67  print 'No CSV file given.'
68  sys.exit(0)
69 
70  csv_file = args[0]
71 
72  if not csv_file.endswith('.csv'):
73  print 'File %s is not a CSV file. Aborting.' % csv_file
74  sys.exit(0)
75 
76  if options.max:
77  output_file = make_sparse_length(csv_file, 65000)
78  elif options.length is None:
79  output_file = make_sparse_skip(csv_file, options.skip)
80  else:
81  output_file = make_sparse_length(csv_file, int(options.length))
82 
83  print 'Created sparse CSV %s' % output_file
def make_sparse_length(csv_file, length)
Makes sparse CSV with the given number of rows.
Definition: sparse.py:70
def make_sparse_skip(csv_file, skip)
Makes sparse CSV by skipping every nth value.
Definition: sparse.py:48


diagnostic_analysis
Author(s): Kevin Watts, Brice Rebsamen , Eric Berger, Kevin Watts
autogenerated on Mon Feb 28 2022 22:18:15