cape.attdb.ftypes.csfilev
: Comma-separated value read/write¶
This module contains a basic interface in the spirit of
cape.attdb.ftypes
for standard comma-separated value files. It
creates a class, CSVFile
that does not rely on the popular
numpy.loadtxt()
function.
If possible, the column names (which become keys in the
dict
-like class) are read from the header row. If the file
begins with multiple comment lines, the column names are read from the
final comment before the beginning of data.
- class cape.attdb.ftypes.csvfile.CSVFile(fname=None, **kw)¶
Class for reading CSV files
- Call:
>>> db = CSVFile(fname, **kw) >>> db = CSVFile(f, **kw)
- Inputs:
- fname:
str
Name of file to read
- f:
file
Open file handle
- fname:
- Outputs:
- db:
cape.attdb.ftypes.csvfile.CSVFile
CSV file interface
- db.cols:
list
[str
] List of columns read
- db.opts:
CSVFileOpts
Options for this instance
- db.defns:
dict
[CSVFileDefn
] Definitions for each column
- db[col]:
np.ndarray
|list
Numeric array or list of strings for each column
- db:
- See also:
- Versions:
2019-11-12
@ddalle
: Version 1.02019-11-26
@ddalle
: Version 2.0; generalized
- c_read_csv(fname, **kw)¶
Read an entire CSV file, including header using C
- Call:
>>> db.read_csv(fname)
- Inputs:
- db:
cape.attdb.ftypes.csvfile.CSVFile
CSV file interface
- fname:
str
Name of file to read
- db:
- See Also:
- Versions:
2019-11-25
@ddalle
: Version 1.0
- c_read_csv_data(f)¶
Read data portion of CSV file using C extension
- Call:
>>> db.c_read_csv_data(f)
- Inputs:
- db:
cape.attdb.ftypes.csvfile.CSVFile
CSV file interface
- f:
file
Open file handle
- db:
- Effects:
- db.cols:
list
[str
] List of column names
- db.cols:
- Versions:
2019-11-25
@ddalle
: Version 1.0
- create_c_dtypes()¶
Initialize db._c_dtypes for C text input
- Call:
>>> db.create_c_dtypes()
- Inputs:
- db:
cape.attdb.ftypes.csvfile.CSVFile
CSV file interface
- db:
- Effects:
- db._c_dtypes:
list
[int
] List of integer codes for each data type
- db._c_dtypes:
- Versions:
2019-11-29
@ddalle
: Version 1.0
- parse_2d_cols()¶
Compress 2D column names into 2D arrays
For example, if cols named
a[0]
anda[1]
are found, it will replace those with a columna
which is the same asnp.stack((db["a[0]"], db["a[1]"), axis=1)
- Call:
>>> db.parse_2d_cols()
- Inputs:
- db:
cape.attdb.ftypes.csvfile.CSVFile
CSV file interface
- db:
- Versions:
2023-09-26
@ddalle
: v1.0
- py_read_csv(fname)¶
Read an entire CSV file with pure Python
- Call:
>>> db.py_read_csv(fname)
- Inputs:
- db:
cape.attdb.ftypes.csvfile.CSVFile
CSV file interface
- fname:
str
Name of file to read
- db:
- See Also:
- Versions:
2019-11-25
@ddalle
: Version 1.0
- py_read_csv_data(f)¶
Read data portion of CSV file using Python
- Call:
>>> db.py_read_csv_data(f)
- Inputs:
- db:
cape.attdb.ftypes.csvfile.CSVFile
CSV file interface
- f:
file
Open file handle
- db:
- Effects:
- db.cols:
list
[str
] List of column names
- db.cols:
- Versions:
2019-11-25
@ddalle
: Version 1.0
- read_csv(fname)¶
Read a CSV file, including header
Reads either entire file or from current location
- Call:
>>> db.read_csv(f) >>> db.read_csv(fname)
- Inputs:
- db:
cape.attdb.ftypes.csvfile.CSVFile
CSV file interface
- f:
file
File open for reading
- fname:
str
Name of file to read
- db:
- See Also:
- Versions:
2019-11-25
@ddalle
: Version 1.0
- read_csv_data(f)¶
Read data portion of CSV file
- Call:
>>> db.read_csv_data(f)
- Inputs:
- db:
cape.attdb.ftypes.csvfile.CSVFile
CSV file interface
- f:
file
Open file handle
- db:
- Effects:
- db.cols:
list
[str
] List of column names
- db.cols:
- Versions:
2019-11-25
@ddalle
: Version 1.02019-11-29
@ddalle
: Tries C versionfirst
- read_csv_dataline(f)¶
Read one data line of a CSV file
- Call:
>>> db.read_csv_dataline(f)
- Inputs:
- db:
cape.attdb.ftypes.csvfile.CSVFile
CSV file interface
- f:
file
Open file handle
- db:
- Versions:
2019-11-25
@ddalle
: Version 1.0
- read_csv_firstrowtypes(f)¶
Get initial guess at data types from first data row
If (and only if) the DefaultType input is an integer type, guessed types can be integers. Otherwise the sequence of possibilities is
float
,complex
,str
.- Call:
>>> db.read_csv_firstrowtypes(f, **kw)
- Inputs:
- db:
cape.attdb.ftypes.csvfile.CSVFile
CSV file interface
- f:
file
Open file handle
- DefaultType: {
"float"
} |str
Name of default class
- db:
- Versions:
2019-11-25
@ddalle
: Version 1.0
- read_csv_header(f)¶
Read column names from beginning of open file
- Call:
>>> db.read_csv_header(f)
- Inputs:
- db:
cape.attdb.ftypes.csvfile.CSVFile
CSV file interface
- f:
file
Open file handle
- db:
- Effects:
- db.cols:
list
[str
] List of column names
- db.cols:
- Versions:
2019-11-12
@ddalle
: Version 1.0
- read_csv_headerdefaultcols(f)¶
Create column names “col1”, “col2”, etc. if needed
- Call:
>>> db.read_csv_headerdefaultcols(f)
- Inputs:
- db:
cape.attdb.ftypes.csvfile.CSVFile
CSV file interface
- f:
file
Open file handle
- db:
- Effects:
- db.cols:
list
[str
] If not previously determined, this becomes
["col1", "col2", ...]
based on number of columns in the first data row
- db.cols:
- Versions:
2019-11-27
@ddalle
: Version 1.0
- read_csv_headerline(f)¶
Read line and process column names if possible
- Call:
>>> db.read_csv_headerline(f)
- Inputs:
- db:
cape.attdb.ftypes.csvfile.CSVFile
CSV file interface
- f:
file
Open file handle
- db:
- Effects:
- db.cols:
None
|list
[str
] List of column names if read
- db._csv_header_once:
True
|False
Set to
True
if column names are read at all- db._csv_header_complete:
True
|False
Set to
True
if next line is expected to be data
- db.cols:
- Versions:
2019-11-22
@ddalle
: Version 1.0
- write_csv(fname, cols=None, **kw)¶
Write a comma-separated file of some of the coefficients
- Call:
>>> db.write_csv(fname, cols=None, **kw)
- Inputs:
- db:
cape.attdb.ftypes.csvfile.CSVFile
CSV file interface
- fname:
str
Name of CSV file to write
- cols: {
None
} |list
(str
) List of coefficients to write, or write all coefficients
- fmt: {
None
} |str
Format string to be used for each row (optional)
- fmts:
dict
|str
Dictionary of formats to use for each coeff
- comments: {
"#"
} |str
Comment character, used as first character of file
- delim: {
", "
} |str
Delimiter
- translators: {
{}
} |dict
Dictionary of col name translations, e.g. CAF -> CA; this dictionary is run in reverse
- db:
- Versions:
2018-06-11
@ddalle
: Version 1.02020-01-15
@jmeeroff
: Fromcape.attdb.db.db1
2020-04-01
@ddalle
: Full options, version 2.0
- write_csv_dense(fname=None, cols=None)¶
Write dense CSV file using WriteFlag for each column
- Call:
>>> db.write_csv_dense(f, cols=None) >>> db.write_csv_dense(fname=None, cols=None)
- Inputs:
- db:
cape.attdb.ftypes.csvfile.CSVFile
CSV file interface
- f:
file
File open for writing
- fname: {db.fname} |
str
Name of file to write
- cols: {db.cols} |
list
[str
] List of columns to write
- db:
- Versions:
2019-12-05
@ddalle
: Version 1.0
- class cape.attdb.ftypes.csvfile.CSVFileDefn(_optsdict=None, _warnmode=1, **kw)¶
- class cape.attdb.ftypes.csvfile.CSVFileOpts(_optsdict=None, _warnmode=1, **kw)¶
- class cape.attdb.ftypes.csvfile.CSVSimple(fname=None, **kw)¶
Class to read CSV file with only
float
dataThis class differs from
CSVFile
in that it is less flexible, does not permit multirow or empty headers, has fixed delimiter and comment characters, and assumes all data is afloat
with the system default length.- Call:
>>> db = CSVSimple(fname, **kw)
- Inputs:
- fname:
str
Name of file to read
- fname:
- Outputs:
- db:
cape.attdb.ftypes.csvfile.CSVSimple
CSV file interface
- db.cols:
list
[str
] List of columns read
- db.opts:
dict
Options for this instance
- db.opts[“Definitions”]:
dict
Definitions for each column
- db[col]:
np.ndarray
|list
Numeric array or list of strings for each column
- db:
- See also:
- Versions:
2019-11-26
@ddalle
: Started
- read_csvsimple(fname)¶
Read an entire CSV file, including header
The CSV file requires exactly one header row, which is the first non-empty line, whether or not it begins with a comment character (which must be
"#"
). All entries, both in the header and in the data, must be separated by a,
.- Call:
>>> db.read_csvsimple(fname)
- Inputs:
- db:
cape.attdb.ftypes.csvfile.CSVSimple
CSV file interface
- fname:
str
Name of file to read
- db:
- See Also:
- Versions:
2019-11-27
@ddalle
: Version 1.0
- read_csvsimple_data(f)¶
Read data portion of simple CSV file
- Call:
>>> db.read_csvsimple_data(f)
- Inputs:
- db:
cape.attdb.ftypes.csvfile.CSVSimple
CSV file interface
- f:
file
Open file handle
- db:
- Effects:
- db.cols:
list
[str
] List of column names
- db.cols:
- Versions:
2019-11-25
@ddalle
: Version 1.0
- read_csvsimple_dataline(f)¶
Read one data line of a simple CSV file
- Call:
>>> db.read_csvsimple_dataline(f)
- Inputs:
- db:
cape.attdb.ftypes.csvfile.CSVSimple
CSV file interface
- f:
file
Open file handle
- db:
- Versions:
2019-11-27
@ddalle
: Version 1.0
- read_csvsimple_header(f)¶
Read column names from beginning of open file
- Call:
>>> db.read_csv_header(f)
- Inputs:
- db:
cape.attdb.ftypes.csvfile.CSVSimple
CSV file interface
- f:
file
Open file handle
- db:
- Effects:
- db.cols:
list
[str
] List of column names
- db.cols:
- Versions:
2019-11-12
@ddalle
: Version 1.0
- translate_simplefloat(txt)¶
Convert a string to default float
This conversion allows for the format
"2.40D+00"
if the built-infloat()
converter fails. Python expects the exponent character to beE
ore
, butD
andd
are allowed here. Other exceptions are not handled.- Call:
>>> v = db.translate_simplefloat(txt)
- Inputs:
- db:
cape.attdb.ftypes.csvfile.CSVFile
CSV file interface
- txt:
str
Text to be converted to
float
- db:
- Outputs:
- v:
float
Converted value, if possible
- v:
- Versions:
2019-11-27
@ddalle
: Version 1.0