Skip to content

Instantly share code, notes, and snippets.

@CaptSolo
Created November 15, 2019 11:22
Show Gist options
  • Save CaptSolo/1ecb4e0a8efcf920ef6a159cfc401ec3 to your computer and use it in GitHub Desktop.
Save CaptSolo/1ecb4e0a8efcf920ef6a159cfc401ec3 to your computer and use it in GitHub Desktop.
How to catch / ignore PyMARC exceptions
#!/bin/python
from pymarc import MARCReader
# Wrapper from https://stackoverflow.com/questions/13653783/how-to-catch-an-exception-in-the-for-loop-iterator
def wrapper(gen):
while True:
try:
yield next(gen)
except StopIteration:
break
except Exception as e:
print(e) # or whatever kind of logging you want
with open('jazz_1k.mrc', 'rb') as fh:
reader = MARCReader(fh, to_unicode=True, force_utf8=True, utf8_handling='replace')
for record in list(wrapper(reader)):
title = record.title()
if not title:
title = "No title"
print(title)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment