Created
November 15, 2019 11:22
-
-
Save CaptSolo/1ecb4e0a8efcf920ef6a159cfc401ec3 to your computer and use it in GitHub Desktop.
How to catch / ignore PyMARC exceptions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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