Package wsatools :: Module CSV
[hide private]

Module CSV

source code

Reading/writing of comma-separated value strings/files. An enhanced interface to the Python standard library csv module, and supports its dialect system to alter the expected standard CSV format, e.g. delimiters.

Usage

Import as:

   import wsatools.CSV as csv

Read/Write CSV Files

To write the results of a database query to a CSV file:

   results = db.query("sourceID, ra, dec", "lasSource")
   csv.File("sources.csv", 'w').writelines(results)

To read a file using the standard dialect:

   for sourceID, Ra, Dec in csv.File("sources.csv"):
       if float(Dec) > 0.0:
           print sourceID
           break

To alter the dialect based on the contents of a file:

   csv.File.autoSetDialect("strange.csv")
   rows = csv.File("strange.csv").readlines()

To manually change the dialect to, say, make the delimiter a single colon character, and to ignore comment lines that start with a double-hyphen:

   from csv import excel
   class ColonDialect(excel):
       delimiter = ':'
   csv.File.setDialect(ColonDialect)
   csv.File.commentMarker = '--'
   rows = csv.File("coloned.csv").readlines()

Convert between sequences and CSV strings

To convert a CSV string to a list:

   elements = list(csv.values('1.0, 2.0, 3.0'))

To convert a list to a CSV string:

   print csv.join([1.0, 2.0, 3.0])

To change dialects:

   import csv as pycsv
   pycsv.register_dialect('strange', csv.sniffDialect('strange.csv'))
   elements = list(csv.values('1.0-2.0-3.0', dialect='strange'))

or:

   pycsv.register_dialect('colon', ColonDialect)
   elements = list(csv.values('1.0:2.0:3.0', dialect='colon'))

To Do: Support dialects on write. Needs a substantial change to the current interface.

Author: R.S. Collins

Organization: WFAU, IfA, University of Edinburgh

Classes [hide private]
  File
Read and write CSV files.
  DbFile
Reads CSV files that have a header line (e.g.
Functions [hide private]
str
join(sequence, inQuotes=True, sep=',', enableMixWarning=True)
Convert a given sequence into a string of comma-separated values.
source code
classobj
sniffDialect(filePathName, numLinesSniffed=10)
Inspect a CSV file to determine its dialect, returning a csv.Dialect object.
source code
tuple(str)
values(csvStr, dialect='excel')
Extract the values from a string containing one line of comma-separated values.
source code
Variables [hide private]
  __package__ = 'wsatools'
Function Details [hide private]

join(sequence, inQuotes=True, sep=',', enableMixWarning=True)

source code 

Convert a given sequence into a string of comma-separated values.

Parameters:
  • sequence (sequence) - A sequence of string-castable objects.
  • inQuotes (bool) - If True, encapsulate strings in quotes.
  • sep (str) - The separator string to use.
  • enableMixWarning (bool) - If True and inQuotes is True then warn if any values contain mixed quotes.
Returns: str
A string of comma-separated values from the list.

sniffDialect(filePathName, numLinesSniffed=10)

source code 

Inspect a CSV file to determine its dialect, returning a csv.Dialect object.

Parameters:
  • filePathName (str) - Full path to the CSV file.
  • numLinesSniffed (int) - Number of lines to inspect.
Returns: classobj
A class derived from csv.Dialect describing the file's dialect.

values(csvStr, dialect='excel')

source code 

Extract the values from a string containing one line of comma-separated values.

Parameters:
  • csvStr (str) - A line of comma-separated values (with or without newlines).
  • dialect (str) - Name of the registered dialect to use.
Returns: tuple(str)
Every value in the line.