daily pastebin goal
1%
SHARE
TWEET

Untitled

a guest Apr 26th, 2018 62 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from pymarc import MARCReader, Record, Field
  2.  
  3. class AlephSequentialReader(MARCReader):
  4.     """
  5.     An iterator class for reading a file of MARC records in Aleph Sequential
  6.     format, which subclasses pymarc's MARCReader. Based on Tim Prettyman's
  7.     MARC::File::AlephSeq Perl code.
  8.    
  9.     """
  10.     def __init__(self, marc_target):
  11.         super(AlephSequentialReader, self).__init__(marc_target)
  12.  
  13.     def next(self):
  14.         """
  15.         To support iteration.
  16.         """
  17.         record_data = ''
  18.         line = self.file_handle.readline()
  19.         if not line:
  20.             raise StopIteration
  21.         key = line[0:9]
  22.         current_key = key
  23.        
  24.         while key == current_key:
  25.             record_data += line
  26.             position = self.file_handle.tell()
  27.             line = self.file_handle.readline()
  28.             key = line[0:9]
  29.        
  30.         self.file_handle.seek(position)
  31.         record = Record()
  32.         for recordln in record_data.splitlines():
  33.             tag = recordln[10:13]
  34.             ind1 = recordln[13:14]
  35.             ind2 = recordln[14:15]
  36.             rest = recordln[18:]
  37.             #if tag == 'FMT': pass
  38.             if tag == 'LDR':
  39.                 record.leader = rest.replace('^', ' ')
  40.             elif tag < '010' and tag.isdigit():
  41.                 if tag == '008': rest = rest.replace('^', ' ')
  42.                 record.add_field(Field(tag=tag, data=rest))
  43.             else:
  44.                 subfields = list()
  45.                 subfield_data = rest.split('$$')
  46.                 subfield_data.pop(0)
  47.                 for subfield in subfield_data:
  48.                     subfields.extend([subfield[0], subfield[1:]])
  49.                 record.add_field(Field(tag=tag, indicators=[ind1, ind2],
  50.                     subfields=subfields))
  51.         return record
RAW Paste Data
Top