Registry / storage / pymarc

pymarc

JSON →
library5.3.1pypypi✓ verified 88d ago

A Python library for reading, writing, and modifying MARC (MAchine-Readable Cataloging) bibliographic data. It supports reading MARC21 from files or strings and writing records in various formats including MARC-8, UTF-8, and XML. Current version 5.3.1, requires Python >=3.9. Released under MIT license.

pip install pymarc
INSTALL
IMPORT
SIG · PYMARC
P
pymarc
storagepythonv5.3.1
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

MARCReader
✓ from pymarc import MARCReader
✗ import pymarc.MARCReader
MARCReader is a class in pymarc, not a module.
Record
✓ from pymarc import Record
✗ from pymarc.record import Record
Record is exported from pymarc directly.
Field
✓ from pymarc import Field
✗ from pymarc.field import Field
Field is exported from pymarc directly.

Open a MARC file, iterate records, print title and author; create a new record and output raw MARC.

from pymarc import MARCReader # Read a MARC file with open('input.mrc', 'rb') as fh: reader = MARCReader(fh) for record in reader: # Access title field (245) title_field = record['245'] if title_field: print(title_field.value()) # Access author (100) author = record['100'] if author: print(author.value()) # Create a new record from pymarc import Record, Field record = Record() record.add_field( Field(tag='245', indicators=['0', '0'], subfields=['a', 'The quick brown fox']) ) raw = record.as_marc() print(repr(raw))
Debug
Known issues
breakingpymarc 5.0.0 dropped Python 3.8 and earlier. Ensure you are on Python 3.9+.
fix
Upgrade Python to 3.9+ or stick with pymarc <5.0 if on Python 3.8.
affects: >=5.0.0
deprecatedThe 'strict' parameter in MARCReader (default True) enforces valid MARC-8 encoding. If you encounter invalid characters, setting strict=False allows reading malformed records.
fix
Use MARCReader(fh, strict=False) for tolerant reading.
affects: all
gotchaRecord.as_marc() returns bytes, not str. Writing to text mode files will cause TypeError.
fix
Open output files in binary mode 'wb' when writing as_marc().
affects: all
gotchaField.value() returns the concatenated subfield values. For specific subfields, use get_subfields() or iterate subfields.
fix
Use field.get_subfields('a') to get list of values for a given subfield code.
affects: all
Errors
Common errors & fixes
TypeError: a bytes-like object is required, not 'str'
Opening MARC file in text mode ('r') instead of binary mode ('rb').
fix
Use open('file.mrc', 'rb') when reading MARC binary files.
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x...
MARC file may not be UTF-8 encoded. pymarc expects UTF-8 by default but MARC-8 is common.
fix
Use encoding='utf-8' explicitly or convert file to UTF-8. For MARC-8, pymarc handles it internally if read as bytes.
AttributeError: 'NoneType' object has no attribute 'value'
Record['245'] returned None if the field is missing.
fix
Check if field exists: title = record['245']; if title: print(title.value()).
ImportError: cannot import name 'Record' from 'pymarc'
Trying to import from submodule instead of top-level package.
fix
Use 'from pymarc import Record' (it's exported in __init__.py).
Upgrade
Version history
5.3.1latest on PyPI · released Jun 18, 2025
Audit
Dependencies

No dependency data recorded yet.

Agent activity
34 hits · last 30 days
node
32
OpenAI (training)
1
Resources

No resource links recorded.

pymarc — pip install pymarc · libregistry