Example #1
0
def iterIsoRecords(iso_file_name, subfields):
    from iso2709 import IsoFile
    def parse(field):
        content = field.value.decode(INPUT_ENCODING,'replace')
        parts = content.split(SUBFIELD_DELIMITER)
        subs = {}
        main = parts.pop(0)
        if len(main) > 0:
            subs['_'] = main
        for part in parts:
            prefix = part[0]
            subs[prefix] = part[1:]
        return subs

    iso = IsoFile(iso_file_name)
    for record in iso:
        fields = {}
        for field in record.directory:
            field_key = str(int(field.tag)) # remove leading zeroes
            field_occurrences = fields.setdefault(field_key,[])
            if subfields:
                field_occurrences.append(parse(field))
            else:
                field_occurrences.append(field.value.decode(INPUT_ENCODING,'replace'))

        yield fields
    iso.close()
def iterIsoRecords(iso_file_name, isis_json_type):
    from iso2709 import IsoFile
    from subfield import expand

    iso = IsoFile(iso_file_name)
    for record in iso:
        fields = {}
        for field in record.directory:
            field_key = str(int(field.tag)) # remove leading zeroes
            field_occurrences = fields.setdefault(field_key,[])
            content = field.value.decode(INPUT_ENCODING,'replace')
            if isis_json_type == 1:
                field_occurrences.append(content)
            elif isis_json_type == 2:
                field_occurrences.append(expand(content))
            elif isis_json_type == 3:
                field_occurrences.append(dict(expand(content)))
            else:
                raise NotImplementedError('ISIS-JSON type %s conversion not yet implemented for .iso input' % isis_json_type)

        yield fields
    iso.close()