MARC21 to CSV

A coworker at MFPOW wanted me to generate a list of records matching certain diverse criteria from our Horizon database. I wrote the following SQL query to get the data out.

SELECT DISTINCT item.bib#
FROM item, bib WHERE bib.bib# = item.item#
AND item.collection NOT IN ('oh', 'icos', 'mi')
AND item.location = 'icos'
AND (bib.text LIKE '%audio%' OR bib.text LIKE '%video%'
OR bib.text LIKE '%cassette%' OR bib.text LIKE '%tape%'
OR bib.text LIKE '%recording%' OR bib.text LIKE '%film%')

I then slapped together this nasty little bit of Python to get it into Excel for easy viewing and formatting.

from pymarc import MARCReader, marc8_to_unicode
import csv, sys
 
r = MARCReader(file(sys.argv[1]))
w = open(sys.argv[2], 'wt')
 
try:
        writer = csv.writer(w, lineterminator='\n')
        writer.writerow(('Bib #','Main Entry','Title'))
        for record in r:
                try: creator = marc8_to_unicode(record.author())
                except: creator = u' '
                creator = creator.encode('iso-8859-1', 'ignore')
                title = marc8_to_unicode(record['245'].formatField())
                title = title.encode('iso-8859-1', 'ignore')
                writer.writerow((record['998']['b'], creator, title))
 
finally: w.close()

No comments

Please register or sign in to post a comment.