MARC21.py

Description
This library provides classes for manipulating MARC21 formatted records. (Should work for USMARC, etc, too.)
The MARC21 file format standards can be found in the MARC Record Structure document at the Library of Congress MARC site.



Version
This document covers version 1.0.1 October 30,2000.

Version 1.0.1

This version fixes a bug that deals with the repitition of MARC21 fields and subfields. Please be aware that fields (ie. M['450']) can be either a single instance of a MARC21DataField or a list of instances. Similarly with the contents of subfields with in a field. When writing programs that deal with arbitrary MARC21 records, use type checking.



Author and copyright
Written by Nathan Denny. This code is in the public domain. No warranty on correctness.


Example
#--MARC files are inherently sequential. This example will open
#--a MARC file, read each record out of it, check for a pattern in the
#--title (245 subfield 'a') and write matching records to a different file.
import MARC21
import string
 
inFile=MARC21.MARC21File('marc.in')
matchingRecords=[]
R=inFile.next()
while R is not None:
   if string.find('halloween',R['245']['a']):
      matchingRecords.append(R)
   R=inFile.next()
 
outFile=open('marc.out','wb')
for R in matchingRecords:
   R['245']['a']='Scary ghosts and goblins'
   outFile.write('%s'%R)
outFile.close()



MARC21.MARC21Record

__init__(self,data=None)

With data=None this initializes the MARC21Record to an empty state. If data is not None, then the data is used to construct the MARC21Record.
fields(self)
Returns a list of fields that are present in this MARC record.



MARC21.MARC21DataField

__init__(self,data=None)

If data is None, then the MARC21DataField is constructed as a blank field. If data is supplied, then the field is constructed accordingly. This is currently used exclusively by the MARC21Record class.
subfields(self)
Returns a list of subfields that are currently in this field.



MARC21.MARC21File

__init__(self,filename)

Opens the specified file for read access.
next(self)
Reads from the current position in the MARC21File and returns a MARC21Record instance based upon the data in the file. If the file is at EOF, None is returned.
rewind(self,n=1)
Rewinds n records from the current position, or until BOF, whichever comes first.