Skip to content

Instantly share code, notes, and snippets.

@EthanDF
Created June 3, 2015 20:31
Show Gist options
  • Save EthanDF/5bbf64441560c250f490 to your computer and use it in GitHub Desktop.
Save EthanDF/5bbf64441560c250f490 to your computer and use it in GitHub Desktop.
replaces whatever is in the 856 tags in the MARC record with the value provided in the CSV file after matching the 001 with the first column.
import csv
from pymarc import *
marcFile = 'C:\\Users\\Fenichele\\Desktop\\fasdocl.mrc'
resultsMarcFile = 'C:\\Users\\Fenichele\\Desktop\\x_fasdocl.mrc'
urlFile = 'C:\\Users\\Fenichele\\Desktop\\em.csv'
def readCSV():
urlList = []
with open(urlFile, 'r') as c:
reader = csv.reader(c)
for row in reader:
urlList.append(row)
return urlList
def returnURL(ocn):
urlList = readCSV()
url = None
for o in urlList:
if ocn == o[0]:
url = o[1]
return url
def remove856(record):
listof856 = record.get_fields('856')
for l in listof856:
record.remove_field(l)
return record
def add856(record,urlUse):
add856 = Field(tag='856', indicators='40', subfields=['u', urlUse])
record.add_field(add856)
return record
def updateMarc(record):
with open(resultsMarcFile, 'ab') as x:
try:
marc = record.as_marc()
marc = marc.decode('utf-8')
x.write(bytes(marc, 'utf-8'))
except UnicodeEncodeError:
ocn = record['001'].value()
ocn = ocn.strip('ocn')
ocn = ocn.strip('ocm')
ocn = ocn.strip(' ')
print("couldn't write "+ocn)
def readMarc():
with open(marcFile, 'rb') as fh:
reader = MARCReader(fh)
for record in reader:
record.force_utf8 = True
# (print['001'], record['856'])
ocn = record['001'].value()
ocn = ocn.strip('ocn')
ocn = ocn.strip('ocm')
ocn = ocn.strip(' ')
urlUse = returnURL(ocn)
if urlUse is None:
print ("no url found for ", ocn)
return record
record = remove856(record)
record = add856(record, urlUse)
updateMarc(record)
return record
readMarc()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment