├── LICENSE ├── OslerPamphlets_pyMarc.py ├── README.md ├── bibSansItems_pymarc_2.py ├── cleanOslerNotes_PyMarc.py ├── cleanOslerNotes_PyMarc_Functions.py └── pyMarc_serials_2.py /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 ClaraTurp 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /OslerPamphlets_pyMarc.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | import csv 4 | import pymarc 5 | from pymarc import MARCReader 6 | from pymarc import MARCWriter 7 | from pymarc import Record 8 | from pymarc.field import Field 9 | from pymarc import marc8 10 | import os 11 | import copy 12 | 13 | 14 | ################### 15 | # Functions 16 | ################### 17 | 18 | def readCsvFile(fileName): 19 | myFile = open(fileName, "r", encoding = "utf-8", errors = "ignore") 20 | reader = csv.reader(myFile) 21 | 22 | return reader 23 | 24 | def createPrintFiles(filename, x): 25 | myFile = open(filename, x, newline = "", encoding="utf-8", errors= "ignore") 26 | writer = csv.writer(myFile) 27 | 28 | return writer 29 | 30 | 31 | def transferToArray(reader, myArray): 32 | for row in reader: 33 | myArray.append(row[0]) 34 | 35 | def cleanOclcNum(array): 36 | matchFound = False 37 | for currentField035a in array: 38 | currentField035a = currentField035a.strip() 39 | if currentField035a.startswith("(OCoLC)"): 40 | oclcNum = currentField035a[7:] 41 | matchFound = True 42 | if matchFound == False: 43 | oclcNum = "; ".join(str(x) for x in array) 44 | return oclcNum 45 | 46 | def cleanSysNumber(SystemNumber): 47 | if SystemNumber.startswith("=001"): 48 | SystemNumber = SystemNumber[4:] 49 | SystemNumber = SystemNumber.strip() 50 | return SystemNumber 51 | 52 | def assignOneField(record, field, subfield): 53 | try: 54 | myField = record[field][subfield] 55 | except TypeError: 56 | myField = "" 57 | return myField 58 | 59 | def assignMultipleFieldsSubfield(record, field, subfield): 60 | myArray = [] 61 | for currentField in record.get_fields(field): 62 | for currentSubfield in currentField.get_subfields(subfield): 63 | myArray.append(currentSubfield) 64 | return myArray 65 | 66 | def assignMultipleSubfields(record, field, subfield1, subfield2, subfield3): 67 | myArray = [] 68 | for currentField in record.get_fields(field): 69 | myArray = [] 70 | for currentSubfield in currentField.get_subfields(subfield1, subfield2, subfield3): 71 | myArray.append(currentSubfield) 72 | return myArray 73 | 74 | 75 | ################### 76 | # Main Code 77 | ################### 78 | 79 | writer = createPrintFiles("OslerPamphlet.csv", "w") 80 | currentDir = os.getcwd() 81 | filesDir = currentDir + "\\MarcFiles\\" 82 | 83 | 84 | for filename in os.listdir(filesDir): 85 | print(filename) 86 | 87 | file = open(filesDir + filename, "rb") 88 | reader = MARCReader(file, to_unicode= True, force_utf8= True, utf8_handling="strict") 89 | 90 | for record in reader: 91 | 92 | SystemNum = str(record["001"]) 93 | SysNum = cleanSysNumber(SystemNum) 94 | oclc = assignMultipleFieldsSubfield(record, "035", "a") 95 | OclcNum = cleanOclcNum(oclc) 96 | location = assignMultipleSubfields(record, "852", "b", "c", "h") 97 | 98 | if len(location) >= 3 and location != None: 99 | if location[0] == "OSLER" and (location[1] == "MAIN" or location[1] == "FELLW") and location[2] == "Pamphlet": 100 | writer.writerow([SysNum, OclcNum, location]) 101 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # PyMARCExamples 2 | A few scripts written during a system migration that use PyMARC 3 | 4 | bibSansItems_pymarc_2.py: Script that creates a 930 field with the appropriate LHR information for a migration. 5 | pyMarc_serials_2.py: Script that manipulates serial records 6 | -------------------------------------------------------------------------------- /bibSansItems_pymarc_2.py: -------------------------------------------------------------------------------- 1 | import csv 2 | import pymarc 3 | from pymarc import MARCReader 4 | from pymarc import MARCWriter 5 | from pymarc import Record 6 | from pymarc.field import Field 7 | import datetime 8 | 9 | 10 | ################### 11 | # Functions 12 | ################### 13 | 14 | def readCsvFile(fileName): 15 | myFile = open(fileName, "r", encoding = "utf-8", errors = "ignore") 16 | reader = csv.reader(myFile) 17 | 18 | return reader 19 | 20 | def readTextFile(fileName): 21 | myFile = open(fileName, "r", encoding = "utf-8", errors = "ignore") 22 | reader = myFile.read() 23 | 24 | return reader 25 | 26 | def createPrintFiles(filename, x): 27 | myFile = open(filename, x, newline = "", encoding="utf-8", errors= "ignore") 28 | writer = csv.writer(myFile) 29 | 30 | return writer 31 | 32 | def transferToLoansArray(reader): 33 | myArray = [] 34 | for row in reader: 35 | myArray.append([row[0], row[1], row[2]]) 36 | return myArray 37 | 38 | def transferDeletedItems(reader): 39 | deletedItems = [] 40 | deletedItems = reader.split("\n") 41 | return deletedItems 42 | 43 | def cleanSysNumber(SystemNumber): 44 | if SystemNumber.startswith("=001"): 45 | SystemNumber = SystemNumber[4:] 46 | SystemNumber = SystemNumber.strip() 47 | return SystemNumber 48 | 49 | 50 | def treatLeader(myLeader): 51 | leaderInfo = leader[7] 52 | if leaderInfo == "m" or leaderInfo == "a" or leaderInfo == "c" or leaderInfo == "i": 53 | mySubfieldM = "BOOK" 54 | elif leaderInfo == "s" : 55 | mySubfieldM = "ISSUE" 56 | else: 57 | mySubfieldM = "FIND ME" 58 | return mySubfieldM 59 | 60 | 61 | def assignOneField(record, field, subfield): 62 | try: 63 | myField = record[field][subfield] 64 | except TypeError: 65 | myField = "" 66 | return myField 67 | 68 | def assignMultipleFieldsSubfield(record, field, subfield): 69 | myArray = [] 70 | for currentField in record.get_fields(field): 71 | for currentSubfield in currentField.get_subfields(subfield): 72 | myArray.append(currentSubfield) 73 | return myArray 74 | 75 | def assignMultipleSubfields(record, field, subfield1, subfield2, subfield3): 76 | myArray = [] 77 | for currentField in record.get_fields(field): 78 | myArray = [] 79 | for currentSubfield in currentField.get_subfields(subfield1, subfield2, subfield3): 80 | myArray.append(currentSubfield) 81 | return myArray 82 | 83 | def dealWithSubfields(myArray, systemNumber, multiplesDict): 84 | if len(myArray) > 1: 85 | mySubfield = myArray[0] 86 | if systemNumber not in multiplesDict: 87 | multiplesDict[systemNumber] = myArray 88 | else: 89 | multiplesDict[systemNumber] = multiplesDict[systemNumber] + myArray 90 | elif len(myArray) == 1 : 91 | mySubfield = myArray[0] 92 | elif len(myArray) == 0: 93 | mySubfield = "" 94 | return mySubfield 95 | 96 | 97 | def createBarcode(number): 98 | barcode = "CTC" 99 | 100 | x = 8 - len(str(number)) 101 | myStr = "" 102 | for i in range(0, x): 103 | myStr = myStr + "0" 104 | 105 | barcode = barcode + myStr + str(number) 106 | return barcode 107 | 108 | def findLoanStatus(subfield1, subfield2, LoanStatusList): 109 | loanStatus = "" 110 | for currentArray in LoanStatusList: 111 | if subfield1 == currentArray[0] and subfield2 == currentArray[1]: 112 | loanStatus = currentArray[2] 113 | return loanStatus 114 | 115 | 116 | ################### 117 | # Main Code 118 | ################### 119 | 120 | LoanCodeDict = { 121 | "01": "Regular Loan", 122 | "02": "Journal Loan", 123 | "04": "In Library Use", 124 | "05": "By Consultation", 125 | "07": "No Circ Info", 126 | "08" : "In Library Use", 127 | "20": "2 day Reserve Loan", 128 | "21": "Regular Loan", 129 | "50" : "Audio/Video & Map Loan", 130 | "x" : "Find me"} 131 | 132 | multiple930Needed = {} 133 | deletedItems = [] 134 | 135 | loanStatusReader = readCsvFile("LoanStatuses.csv") 136 | LoanStatusList = transferToLoansArray(loanStatusReader) 137 | 138 | deletedItemsreader = readTextFile("deleted_items_list_20190110.txt") 139 | deletedItems = transferDeletedItems(deletedItemsreader) 140 | 141 | SerialsRecordsWriter = MARCWriter(open("itemCreatedSerials.mrc", "wb")) 142 | BooksRecordsWriter = MARCWriter(open("itemCreatedBooks.mrc", "wb")) 143 | myErrorFile = createPrintFiles("myErrorFile.csv", "w") 144 | mydeletesFile = createPrintFiles("deletedItemsFile.csv", "w") 145 | myMultipleFile = createPrintFiles("multiple930Needed.csv", "w") 146 | myWriterFile = createPrintFiles("itemsCreated.csv", "w") 147 | 148 | file = open("bibssansitems_with852.mrc", "rb") 149 | reader = MARCReader(file, to_unicode= True, force_utf8= True, utf8_handling="strict") 150 | 151 | number = 1 152 | for record in reader: 153 | 154 | if record["852"] and not record["930"]: 155 | 156 | SystemNum = str(record["001"]) 157 | SysNum = cleanSysNumber(SystemNum) 158 | 159 | if SysNum not in deletedItems: 160 | 161 | collection = assignMultipleFieldsSubfield(record, "852", "b") 162 | subfield1 = dealWithSubfields(collection, SysNum, multiple930Needed) 163 | 164 | shelvingLocation = assignMultipleFieldsSubfield(record, "852", "c") 165 | subfield2 = dealWithSubfields(shelvingLocation, SysNum, multiple930Needed) 166 | 167 | if subfield2 == "": 168 | myErrorFile.writerow([SysNum, "No Specific Location Information (852$c)"]) 169 | elif subfield1 == "DBASE": 170 | mydeletesFile.writerow([SysNum, "DBASE"]) 171 | else: 172 | subfieldl = "MGU01" 173 | subfieldL = "MUSE" 174 | 175 | leader= record.leader 176 | subfieldm = treatLeader(leader) 177 | if subfieldm == "FIND ME": 178 | myErrorFile.writerow([SysNum, "new LDR # 8 value"]) 179 | else: 180 | classification = assignMultipleFieldsSubfield(record, "852", "h") 181 | subfieldh = dealWithSubfields(classification, SysNum, multiple930Needed) 182 | 183 | itemNumber = assignMultipleFieldsSubfield(record, "852", "i") 184 | subfieldi = dealWithSubfields(itemNumber, SysNum, multiple930Needed) 185 | 186 | subfield5 = createBarcode(number) 187 | number = number + 1 188 | 189 | subfield8 = str(datetime.datetime.now().strftime("%y%m%d%H%M%S")) 190 | 191 | subfieldf = findLoanStatus(subfield1, subfield2, LoanStatusList) 192 | 193 | if subfieldf == "x": 194 | myErrorFile.writerow([SysNum, "x loan status code"]) 195 | elif subfieldf == "": 196 | myErrorFile.writerow([SysNum, "Unknown Loan Status"]) 197 | subfieldF = "" 198 | else: 199 | subfieldF = LoanCodeDict[subfieldf] 200 | 201 | holdingsData = str(record["852"]) 202 | subfieldw = holdingsData[6] 203 | 204 | 205 | myfield = Field(tag = "930", indicators= [" ", "1"], subfields=[ 206 | "l", subfieldl, 207 | "L", subfieldL, 208 | "m", subfieldm, 209 | "1", subfield1, 210 | "2", subfield2, 211 | "h", subfieldh, 212 | "i", subfieldi, 213 | "5", subfield5, 214 | "8", subfield8, 215 | "f", subfieldf, 216 | "F", subfieldF, 217 | "w", subfieldw 218 | ]) 219 | 220 | record.add_field(myfield) 221 | myWriterFile.writerow([SysNum, subfieldm, subfield1, subfield2, subfield5, "item added"]) 222 | 223 | if subfieldm == "ISSUE": 224 | SerialsRecordsWriter.write(record) 225 | 226 | elif subfieldm == "BOOK": 227 | BooksRecordsWriter.write(record) 228 | 229 | 230 | else: 231 | mydeletesFile.writerow([SysNum, "a deleted item match"]) 232 | else: 233 | SystemNum = str(record["001"]) 234 | SysNum = cleanSysNumber(SystemNum) 235 | myErrorFile.writerow([SysNum, "no 852 or a 930"]) 236 | 237 | 238 | for keys in multiple930Needed: 239 | myMultipleFile.writerow([keys, multiple930Needed[keys]]) 240 | 241 | SerialsRecordsWriter.close() 242 | BooksRecordsWriter.close() 243 | -------------------------------------------------------------------------------- /cleanOslerNotes_PyMarc.py: -------------------------------------------------------------------------------- 1 | import csv 2 | import pymarc 3 | from pymarc import MARCReader 4 | from pymarc import MARCWriter 5 | from pymarc import Record 6 | from pymarc.field import Field 7 | import datetime 8 | import os 9 | 10 | from cleanOslerNotes_PyMarc_Functions import createPrintFiles, cleanOclcNum, assignMultipleFieldsSubfields 11 | 12 | ################### 13 | # Main Code 14 | ################### 15 | 16 | 17 | OslerNotes = createPrintFiles("oslerNotesStr_Final.csv", "w") 18 | 19 | currentDir = os.getcwd() 20 | filesDir = currentDir + "\\MarcFiles\\" 21 | 22 | 23 | for filename in os.listdir(filesDir): 24 | print(filename) 25 | 26 | file = open(filesDir + filename, "rb") 27 | reader = MARCReader(file, to_unicode= True, force_utf8= True, utf8_handling='ignore') 28 | 29 | for record in reader: 30 | 31 | oclc = assignMultipleFieldsSubfields(record, "035", "a") 32 | oclcNum = cleanOclcNum(oclc) 33 | notes = assignMultipleFieldsSubfields(record, "876", "z") 34 | notes = "/".join(notes) 35 | barcode = assignMultipleFieldsSubfields(record, "930", "5") 36 | barcode = "/".join(barcode) 37 | branch = assignMultipleFieldsSubfields(record, "930", "1") 38 | branch = "/".join(branch) 39 | shelvingLocation = assignMultipleFieldsSubfields(record, "930", "2") 40 | shelvingLocation = "/".join(shelvingLocation) 41 | 42 | if len(notes) > 0: 43 | if "ROBE" in shelvingLocation: 44 | myRecord = [oclcNum, barcode, branch, shelvingLocation, notes] 45 | OslerNotes.writerow(myRecord) 46 | -------------------------------------------------------------------------------- /cleanOslerNotes_PyMarc_Functions.py: -------------------------------------------------------------------------------- 1 | import csv 2 | import pymarc 3 | from pymarc import MARCReader 4 | from pymarc import MARCWriter 5 | from pymarc import Record 6 | from pymarc.field import Field 7 | import datetime 8 | import os 9 | 10 | ################### 11 | # Functions 12 | ################### 13 | 14 | def createPrintFiles(filename, x): 15 | myFile = open(filename, x, newline = "", encoding="utf-8", errors= "ignore") 16 | writer = csv.writer(myFile) 17 | 18 | return writer 19 | 20 | 21 | def cleanOclcNum(array): 22 | matchFound = False 23 | for currentField035a in array: 24 | currentField035a = currentField035a.strip() 25 | if currentField035a.startswith("(OCoLC)"): 26 | oclcNum = currentField035a[7:] 27 | matchFound = True 28 | if matchFound == False: 29 | oclcNum = "; ".join(str(x) for x in array) 30 | return oclcNum 31 | 32 | 33 | def assignMultipleFieldsSubfields(record, field, subfield): 34 | myArray = [] 35 | for currentField in record.get_fields(field): 36 | for currentSubfield in currentField.get_subfields(subfield): 37 | if currentSubfield is None: 38 | myArray.append("no Subfield") 39 | else: 40 | myArray.append(currentSubfield) 41 | return myArray 42 | -------------------------------------------------------------------------------- /pyMarc_serials_2.py: -------------------------------------------------------------------------------- 1 | import csv 2 | import pymarc 3 | from pymarc import MARCReader 4 | from pymarc import MARCWriter 5 | from pymarc import Record 6 | from pymarc.field import Field 7 | import os 8 | import copy 9 | import re 10 | 11 | ################### 12 | # Functions 13 | ################### 14 | 15 | def createPrintFiles(filename): 16 | myFile = open(filename, "w", newline = "", encoding="utf-8", errors= "ignore") 17 | writer = csv.writer(myFile) 18 | 19 | return writer 20 | 21 | 22 | def cleanOclcNum(array): 23 | matchFound = False 24 | for currentField035a in array: 25 | currentField035a = currentField035a.strip() 26 | if currentField035a.startswith("(OCoLC)"): 27 | oclcNum = currentField035a[7:] 28 | matchFound = True 29 | if matchFound == False: 30 | oclcNum = "; ".join(str(x) for x in array) 31 | return oclcNum 32 | 33 | def cleanSysNumber(SystemNumber): 34 | if SystemNumber.startswith("=001"): 35 | SystemNumber = SystemNumber[4:] 36 | SystemNumber = SystemNumber.strip() 37 | return SystemNumber 38 | 39 | def createBarcode(number): 40 | barcode = "CTCC" 41 | 42 | x = 7 - len(str(number)) 43 | myStr = "" 44 | for i in range(0, x): 45 | myStr = myStr + "0" 46 | 47 | barcode = barcode + myStr + str(number) 48 | return barcode 49 | 50 | def assignOneField(record, field, subfield): 51 | try: 52 | myField = record[field][subfield] 53 | except TypeError: 54 | myField = "" 55 | return myField 56 | 57 | def assignMultipleFields(record, field, subfield): 58 | myArray = [] 59 | for currentField in record.get_fields(field): 60 | for currentSubfield in currentField.get_subfields(subfield): 61 | myArray.append(currentSubfield) 62 | return myArray 63 | 64 | def treatMyStatus(record, field, subfield, Status): 65 | myArray = [] 66 | for currentField in record.get_fields(field): 67 | for currentSubfield in currentField.get_subfields(subfield): 68 | if currentSubfield == Status: 69 | myArray.append(currentField) 70 | return myArray 71 | 72 | def removeFields(myArray1, myArray2, myArray3, record): 73 | global rowsDeleted 74 | recordChanged = False 75 | if len(myArray1) > 0: 76 | for currentSlot in myArray1: 77 | if currentSlot != "": 78 | record.remove_field(currentSlot) 79 | recordChanged = True 80 | rowsDeleted = rowsDeleted + 1 81 | if len(myArray2) > 0: 82 | for currentSlot in myArray2: 83 | if currentSlot != "": 84 | record.remove_field(currentSlot) 85 | recordChanged = True 86 | rowsDeleted = rowsDeleted + 1 87 | if len(myArray3) > 0: 88 | for currentSlot in myArray3: 89 | if currentSlot != "": 90 | record.remove_field(currentSlot) 91 | recordChanged = True 92 | rowsDeleted = rowsDeleted + 1 93 | return recordChanged 94 | 95 | def findIterationsOfYears(myAArray, mySArray, myFinalAArray, myFinalSArray): 96 | for currentSubfield in myAArray: 97 | myFinalAArray.extend(re.findall("([12][06-9]\d\d[-\/]?[12]?[06-9]?\d?\d?)", currentSubfield)) 98 | for currentSubfield in mySArray: 99 | myFinalSArray.extend(re.findall("([12][06-9]\d\d[-\/]?[12]?[06-9]?\d?\d?)", currentSubfield)) 100 | 101 | 102 | def treatYearRanges(Separator, myNote, myArray): 103 | y = myNote.split(Separator) 104 | if len(y[1]) == 4: 105 | for numbers in range(int(y[0]), int(y[1]) + 1): 106 | if not str(numbers).startswith("10"): 107 | myArray.append(str(numbers)) 108 | elif len(y[1]) == 2: 109 | u = int(y[0][:2] + y[1]) + 1 110 | for numbers in range(int(y[0]), u): 111 | if not str(numbers).startswith("10"): 112 | myArray.append(str(numbers)) 113 | elif len(y[1]) == 1: 114 | u = int(y[0][:3] + y[1]) + 1 115 | for numbers in range(int(y[0]), u): 116 | if not str(numbers).startswith("10"): 117 | myArray.append(str(numbers)) 118 | 119 | def rebuildYearArray(myArray): 120 | myCurrentList = myArray 121 | myArray = [] 122 | 123 | for currentIter in myCurrentList: 124 | if "/" in currentIter: 125 | treatYearRanges("/", currentIter, myArray) 126 | elif "-" in currentIter: 127 | treatYearRanges("-", currentIter, myArray) 128 | elif re.match("([12][06-9]\d\d)", currentIter): 129 | if not currentIter.startswith("10"): 130 | myArray.append(currentIter) 131 | 132 | if myArray == []: 133 | myArray = myCurrentList 134 | return myArray 135 | 136 | def cleanUpArrays(myArray): 137 | myArray = list(set(myArray)) 138 | myArray = sorted(myArray) 139 | return myArray 140 | 141 | ################### 142 | # Main Code 143 | ################### 144 | 145 | csvFileWriter = createPrintFiles("coverageAnalysis_Results.csv") 146 | csvFileWriter.writerow(["System Number", "Oclc Number", "Title", "866 subfield A", "930 subfield S", "Analysis result"]) 147 | onlyFileWriter = createPrintFiles("all_explained.csv") 148 | onlyFileWriter.writerow(["System Number", "Oclc Number", "Title","866 subfield A", "930 subfield S", "How was record manipulated"]) 149 | deleted930FileWriter = createPrintFiles("deletedItems.csv") 150 | deleted930FileWriter.writerow(["System Number", "Oclc Number", "Title", "A 930 deleted due to status"]) 151 | 152 | changedRecordsWriter = MARCWriter(open("changedRecord.mrc", "wb")) 153 | unchangedRecordsWriter = MARCWriter(open("unchangedRecord.mrc", "wb")) 154 | deletedRecordsWriter = MARCWriter(open("deletedRecord.mrc", "wb")) 155 | 156 | rowsDeleted = 0 157 | rowsAdded = 0 158 | 159 | 160 | 161 | currentDir = os.getcwd() 162 | filesDir = currentDir + "\\files\\" 163 | 164 | 165 | for filename in os.listdir(filesDir): 166 | print(filename) 167 | sysNumCountArray = [] 168 | originalItemCount = 0 169 | finalItemCount = 0 170 | 171 | file = open(filesDir + filename, "rb") 172 | reader = MARCReader(file, to_unicode = True, force_utf8= True) 173 | 174 | number = 1 175 | 176 | for record in reader: 177 | myRecord = [] 178 | subfieldS = [] 179 | subfieldA = [] 180 | 181 | #Match basic fields 182 | SystemNum = str(record["001"]) 183 | SysNum = cleanSysNumber(SystemNum) 184 | if SysNum not in sysNumCountArray: 185 | sysNumCountArray.append(SysNum) 186 | oclc = assignMultipleFields(record, "035", "a") 187 | OclcNum = cleanOclcNum(oclc) 188 | title = assignOneField(record, "245", "a") 189 | 190 | 191 | items = record.get_fields("930") 192 | originalItemCount = originalItemCount + len(items) 193 | 194 | #Remove the 930's with the Status Not arrived or Claimed 195 | NaStatuses = treatMyStatus(record, "930", "P", "Not arrived") 196 | NAStatuses = treatMyStatus(record, "930", "P", "Not Arrived") 197 | ClStatuses = treatMyStatus(record, "930", "P", "Claimed") 198 | recordChanged = removeFields(NaStatuses, NAStatuses, ClStatuses, record) 199 | if recordChanged == True: 200 | myRecord = [SysNum, OclcNum, title, "A 930 deleted due to status"] 201 | deleted930FileWriter.writerow(myRecord) 202 | 203 | if record["930"] is not None: 204 | 205 | subfieldA = assignMultipleFields(record, "866", "a") 206 | subfieldS = assignMultipleFields(record, "930", "s") 207 | 208 | 209 | if len(subfieldA) > 0 : 210 | #If there are no 930$s and at least one 866$a 211 | if len(subfieldS) == 0: 212 | 213 | if len(subfieldA) > 1: 214 | myField = record["930"] 215 | #Copy 930 fields 216 | for i in range(0, len(subfieldA)-1): 217 | record.add_field(copy.deepcopy(myField)) 218 | rowsAdded = rowsAdded + 1 219 | 220 | #Add the different 866$a 221 | allFields = record.get_fields("930") 222 | if len(allFields) > len(subfieldA): 223 | myRecord = [SysNum, OclcNum, title, subfieldA, subfieldS, "Should be Manually Checked"] 224 | onlyFileWriter.writerow(myRecord) 225 | changedRecordsWriter.write(record) 226 | else: 227 | myRecord = [SysNum, OclcNum, title, subfieldA, subfieldS, "Fake CTCC Barcode Added"] 228 | onlyFileWriter.writerow(myRecord) 229 | 230 | for x in range(1, len(allFields)): 231 | allFields[x].delete_subfield("5") 232 | myBarcode = createBarcode(number) 233 | number = number + 1 234 | allFields[x].add_subfield("5", myBarcode) 235 | 236 | for i in range(0, len(allFields)): 237 | #allFields[i].add_subfield("s", subfieldA[i]) 238 | y = len(allFields[i].subfields)-2 239 | allFields[i].subfields.insert(y, "s") 240 | allFields[i].subfields.insert(y+1, subfieldA[i]) 241 | 242 | changedRecordsWriter.write(record) 243 | 244 | else: 245 | myRecord = [SysNum, OclcNum, title, subfieldA, subfieldS, "Record Changed"] 246 | onlyFileWriter.writerow(myRecord) 247 | 248 | myField = record["930"] 249 | y = len(myField.subfields)-2 250 | myField.subfields.insert(y, "s") 251 | myField.subfields.insert(y+1, subfieldA[0]) 252 | #myField.add_subfield("s", subfieldA[0]) 253 | changedRecordsWriter.write(record) 254 | 255 | #If there is a 930$s 256 | else: 257 | unchangedRecordsWriter.write(record) 258 | myRecord = [SysNum, OclcNum, title, subfieldA, subfieldS, "Unchanged Record"] 259 | onlyFileWriter.writerow(myRecord) 260 | 261 | #Find Records with Coverage Years that don't match (866$a and 930$s) 262 | allAIterations = [] 263 | allSIterations = [] 264 | findIterationsOfYears(subfieldA, subfieldS, allAIterations, allSIterations) 265 | allAIterations = rebuildYearArray(allAIterations) 266 | allSIterations = rebuildYearArray(allSIterations) 267 | 268 | allAIterations = cleanUpArrays(allAIterations) 269 | allSIterations = cleanUpArrays(allSIterations) 270 | 271 | if len(allAIterations) == 0 and len(allSIterations) == 0: 272 | myRecord = [SysNum, OclcNum, title, subfieldA, subfieldS, "No Year Info"] 273 | csvFileWriter.writerow(myRecord) 274 | elif allAIterations == allSIterations: 275 | myRecord = [SysNum, OclcNum, title, subfieldA, subfieldS, "Same Coverage"] 276 | csvFileWriter.writerow(myRecord) 277 | else: 278 | myRecord = [SysNum, OclcNum, title, subfieldA, subfieldS, "Different Coverage"] 279 | csvFileWriter.writerow(myRecord) 280 | 281 | #If there are no $a and no $s 282 | elif len(subfieldA) == 0 and len(subfieldS) == 0: 283 | myRecord = [SysNum, OclcNum, title, subfieldA, subfieldS, "Unchanged Record no coverage info"] 284 | onlyFileWriter.writerow(myRecord) 285 | unchangedRecordsWriter.write(record) 286 | else: 287 | myRecord = [SysNum, OclcNum, title, subfieldA, subfieldS, "Other unchanged record"] 288 | onlyFileWriter.writerow(myRecord) 289 | unchangedRecordsWriter.write(record) 290 | else: 291 | deletedRecordsWriter.write(record) 292 | items = record.get_fields("930") 293 | finalItemCount = finalItemCount + len(items) 294 | 295 | print("Number of records for " + filename + ": " + str(len(sysNumCountArray))) 296 | print("Number of original items for " + filename + ": " + str(originalItemCount)) 297 | print("Final number of items for " + filename + ": " + str(finalItemCount)) 298 | print("Number of deleted rows: " + str(rowsDeleted)) 299 | print("Number of added rows: " + str(rowsAdded)) 300 | unchangedRecordsWriter.close() 301 | changedRecordsWriter.close() 302 | 303 | --------------------------------------------------------------------------------