PyMARC

A MARC parser in Python

This is a Python class for parsing and producing MARC data. It reads and writes both MARC21 format records and records in a mnemonic format very similar to that used by Terry Reese's MarcEdit.

The package also includes simple conversion scripts, MarcMaker.py and MarcBreaker.py. MarcMaker converts from the mnemonic-text format to MARC transport (z39.2), and MarcBreaker does the opposite. They are mostly examples of using the parser.

A PHP translation of this code is used in the OpenBiblio library system.

Example

Read MARC data on stdin and print out the author field (100$a) from each record or 'None' if it doesn't have one.

#!/usr/bin/python
import sys
import MARC

p = MARC.Parser(True)

def handle(r):
	print r.get_value('100$a')

while 1:
	buf = sys.stdin.read(8192)
	if buf=='':
		break
	if p.parse(buf) > 0:
		for r in p.records:
			handle(r)
		p.records=[]
p.eof()
for r in p.records:
	handle(r)