Python - KyleneKristine/meta GitHub Wiki

Export data from MRC

import pymarc
from pymarc import MARCReader
with open(r'C:/path/file', 'rb') as fh: #Select file containing needed information (ex. C:/Users/name/Desktop/test.mrc)
  reader = MARCReader(fh)
  inlist = []
  for record in reader:
    try:
      print(record['010']['a']) #change as needed; line isn't necessary and can be deleted if prefered
      inlist.append(record['010']['a']+'\n') #repeat record number/subfield change here as well
    except TypeError:
      print('Error')
    with open(r'C:/path/file', 'w') as txt: #different file than above, can create brand new file this way. (ex. C:/Users/name/Desktop/test.txt')
      txt.writelines(inlist)

Exporting for Comparison Sheet

import pymarc
from pymarc import MARCReader
with open(r'C:\path\file.mrc', 'rb') as fh:
	reader = MARCReader(fh)
	inlist = []
	for record in reader:
		if record['245'] is not None:
			title = record['245']['a']
			if record['245']['b'] is not None:
				title = title + " " + record['245']['b']
				inlist.append(title)
			elif record['245']['b'] is None:
				inlist.append(title)
		inlist.append('%')
		
		if record['100'] is not None:
			author = record['100']['a']
			inlist.append(author)
		elif record['110'] is not None:
			author = record['110']['a']
			inlist.append(author)
		elif record['700'] is not None:
			author = record['700']['a']
			inlist.append(author)
		elif record['710'] is not None:
			author = record['710']['a']
			inlist.append(author)
		inlist.append('%')

		try:
			if record['260'] is not None:
				publisher = record['260']['b']
				date = record['260']['c']
				inlist.append(publisher + '%' + date)
			elif record['264'] is not None:
				publisher = record['264']['b']
				date = record['264']['c']
				inlist.append(publisher + '%' + date)
		except TypeError:
			inlist.append('%')
		inlist.append('%')
		
		if record['650'] is not None:
			subject = record['650']['a']
			inlist.append(subject)
		inlist.append('%')

		try:
			if record['035']['a'] is not None:
				oclc = record['035']['a']
				inlist.append(oclc)
		except TypeError:
			None

		inlist.append('\n')

		with open(r'C:\path\file.txt', 'w', encoding='utf-8') as txt:
			txt.writelines(inlist)

WOD

from datetime import datetime
with open(r'C:\path\file.txt', 'r') as myfile:
	modstext=[]
	newlist = [line.rstrip() for line in myfile.readlines()]
	counter = 0
	for x in newlist:
		crname = newlist[0+counter]
		genre = newlist[1+counter]
		coname = newlist[2+counter]
		date = newlist[3+counter]
		textent = newlist[4+counter]
			
		title=crname + ' reading ' + genre + ' at the ' + coname
		fulldate=datetime.strptime(date, '%m/%d/%y').strftime('%B %d, %Y')
		extent=textent.split(' ')[3]
		fname=crname.split(' ')[0]
		lname=crname.split(' ')[1]
		crnamer=lname + ', ' + fname
		
		modstext.append('<mods:mods xmlns:mods="http://www.loc.gov/mods/v3" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.loc.gov/mods/v3 http://www.loc.gov/standards/mods/v3/mods-3-4.xsd">' + '\n' + '<mods:titleInfo>' + '\n' + '<mods:title>' + title + '</mods:title>' + '\n' + '</mods:titleInfo>' + '\n' + '<mods:abstract>' + title + ', ' + fulldate +'.</mods:abstract>' + '\n' + '<mods:name>' + '<mods:namePart>' + crnamer + '</mods:namePart>' + '\n' + '<mods:role>' + '\n' + '<mods:roleTerm authority="marcrelator" authorityURI="http://id.loc.gov/vocabulary/relators" valueURI="http://id.loc.gov/vocabulary/relators/cre">Creator</mods:roleTerm>' + '\n' + '</mods:role>' + '\n' + '</mods:name>' + '\n' + '<mods:name>' + '\n' + '<mods:namePart>' + coname + '</mods:namePart>' + '\n' + '<mods:role>' + '\n' + '<mods:roleTerm authority="marcrelator" authorityURI="http://id.loc.gov/vocabulary/relators" valueURI="http://id.loc.gov/vocabulary/relators/ctb">Contributor</mods:roleTerm>' + '\n' + '</mods:role>' + '\n' + '</mods:name>' + '\n' + '<mods:originInfo>' + '\n' + '<mods:dateCreated>' + date + '</mods:dateCreated>' + '\n' + '</mods:originInfo>' + '\n' + '<mods:language>' + '\n' + '<mods:languageTerm type="code" authority="iso639-2b">eng</mods:languageTerm>' + '\n' + '</mods:language>' + '\n' + '<mods:physicalDescription>' + '\n' + '<mods:extent>1 mp3 (' + extent + ')</mods:extent>' + '\n' + '<mods:digitalOrigin>born digital</mods:digitalOrigin>' + '\n' + '</mods:physicalDescription>' + '\n' + '<mods:typeOfResource>sound recording</mods:typeOfResource>' + '\n' + '<mods:genre>' + genre + '</mods:genre>' + '\n' + '</mods:mods>' + '\n \n')
		counter += 6
		with open(r'C:\path\file.txt', 'w') as txt:
			txt.writelines(modstext)
⚠️ **GitHub.com Fallback** ⚠️