pymarc – Python MARC record processing (convert MARCXML to MARC)

This will sound very boring to anyone not involved with Library data, but pymarc totally blew my mind the other day. The library – available from or via pip install pymarc allows super simple processing of MARC data.

My personal need was to quickly and efficiently convert records from MARCXML to MARC21. To do that you obviously need to import the library

import pymarc

And then it can be as simple as:

def convertToMrc(input):
    output = input.replace('output/', 'usmarc/')
    output = output.replace('.xml', '.mrc')
    writer = pymarc.MARCWriter(file(output, 'wb')) 
    records = pymarc.map_xml(writer.write, input) 

To be fair, the last three lines do all the hard work. As I said, this library totally blew my mind and in the past I have used Command Line Tools from MarcEdit for this job, but doing it this way, is very neat and very efficient indeed.

pymarc – Python MARC record processing (convert MARCXML to MARC)
Scroll to top