Skip to content

Instantly share code, notes, and snippets.

@fontanka16
Created September 19, 2019 06:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fontanka16/c13792a8c493abaa1a6e0599e59efd0c to your computer and use it in GitHub Desktop.
Save fontanka16/c13792a8c493abaa1a6e0599e59efd0c to your computer and use it in GitHub Desktop.
'''takes a marc21 dump and adds 999$i fields to it'''
import sys
from pymarc import MARCReader
from pymarc import Field
import json
def main():
stats = {
'records': 0,
'866': 0,
'852': 0,
'852 and 866': 0,
'failed': 0,
'ur': 0
}
with open(sys.argv[1], 'rb') as marc_file:
reader = MARCReader(marc_file, 'rb')
results_file = sys.argv[2]
with open(results_file, 'wb+') as res_file:
for marc_record in reader:
stats['records'] += 1
try:
if ('007' in marc_record and marc_record['007'].format_field().startswith('cr')):
stats['ur'] += 1
else:
f852s = marc_record.get_fields('852')
if (f852s):
stats['852'] += len(f852s)
f866s = marc_record.get_fields('866')
if (f866s):
stats['866'] += len(f866s)
if (f866s and f852s):
stats['852 and 866'] += 1
marc_record.remove_fields('852', '866')
mrc = marc_record.as_marc()
res_file.write(mrc)
except Exception as ee:
print(ee)
stats['failed'] += 1
if stats['records'] % 1000 == 0:
print(stats)
print(stats)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment