Other api/dybdbi/csv

Previous topic

DybDbi.Wrap

Next topic

DybDbi.Source

This Page

Daya Bay Links

Content Skeleton

DybDbi.CSV

class DybDbi.CSV(path, **kwargs)

Bases: list

Reader/writer for .csv files. The contents are stored as a list of dicts.

Parameters:
  • delimiter – csv field divider
  • prefix – string start of comment lines to be ignored, default #Table
  • descmarker – strings used to identify the field description line
  • synth – when defined, add extra field with this name to hold the csv source line number
  • fields – impose fieldnames externally, useful for handling broken csv which cannot be fixed immediately

Read usage example:

src = CSV("$DBWRITERROOT/share/DYB_MC_AD1.txt", delimiter="\t" )
src.read()
for d in src:
    print d

len(src)
src[0]
src[-1]
src.fieldnames

On reading an invalid CSV an exception, with error report, is raised:

src = CSV("$DBWRITERROOT/share/DYB_SAB_AD1.txt", delimiter="\t" )
src.read()

Handling of common csv incorrectnesses is made:

  1. description line fixed up to conform to the delimiter
  2. description line extraneous characters removed (other than fieldnames and delimiters)
  3. removes comments

Write usage example, field names are obtained from the dict keys:

out = CSV("/tmp/demo.csv", delimiter="\t" )
for d in list_of_dict_datasource:
    out.append(d)
out.write()
fieldnames

If fieldnames keyword argument is supplied return that otherwise return the names of the keys in the first contained dict. In order to control the order of fields, the argument has to be specified.

write()