source: coopr.pyomo/trunk/coopr/pyomo/data/csv_table.py @ 2592

Last change on this file since 2592 was 2592, checked in by wehart, 9 years ago

Rework of tests to use new plugin factory mechanism
supported by pyutilib.component.core

File size: 1.8 KB
Line 
1#  _________________________________________________________________________
2#
3#  Coopr: A COmmon Optimization Python Repository
4#  Copyright (c) 2008 Sandia Corporation.
5#  This software is distributed under the BSD License.
6#  Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
7#  the U.S. Government retains certain rights in this software.
8#  For more information, see the Coopr README.txt file.
9#  _________________________________________________________________________
10
11import os.path
12import re
13from TableData import TableData
14import csv
15from pyutilib.component.core import alias
16
17
18class CSVTable(TableData):
19
20    alias("csv", "Manage IO with tables in CSV files.")
21
22    def __init__(self):
23        TableData.__init__(self)
24
25    def open(self):
26        if self.filename is None:
27            raise IOError, "No filename specified"
28        if not os.path.exists(self.filename):
29            raise IOError, "Cannot find file '%s'" % self.filename
30        self.INPUT = open(self.filename, 'r')
31
32    def close(self):
33        self.INPUT.close()
34       
35    def read(self):
36        tmp=[]
37        for tokens in csv.reader(self.INPUT):
38            if tokens != ['']:
39                tmp.append(tokens)
40        if len(tmp) == 0:
41            raise IOError, "Empty *.csv file"
42        elif len(tmp) == 1:
43            if not self.options.param is None:
44                self._info = ["param",self.options.param,":=",tmp[0][0]]
45            elif len(self.options.symbol_map) == 1:
46                self._info = ["param",self.options.symbol_map[self.options.symbol_map.keys()[0]],":=",tmp[0][0]]
47            else:
48                raise IOError, "Data looks like a parameter, but multiple parameter names have been specified: %s" % str(self.options.symbol_map)
49        else:
50            self._set_data(tmp[0], tmp[1:])
51        return True
52
53
54
55
Note: See TracBrowser for help on using the repository browser.