Oren Maurer
Oren Maurer

Reputation: 299

Python pymarc - how to print field tag and each indicator and subfield

I have a MARC21 record in an input file. I am trying to print it in Aleph Sequential format.

How?
I want to print record number (9 digits), space, the field tag, indicators, space, the L letter, space and then each subfield tag and subfield contests.

I am trying to use the record.get_fields(), but I do not succeed in getting the field indicators and subfield tag.

How cat I get and print field indicators? How do I print ech subfield tag and then each subfield value?

Here is my code:

from pymarc import MARCReader
from pymarc import Record, Field 

with open('machiavellism_biu_2018.mrc', 'rb') as fh:
    reader = MARCReader(fh, to_unicode=True) 
    # loop 1
    recnum = 0
    for record in reader: 
        # loop 2
        recnum += 1
        for tield_contents in record.get_fields():
            print ('%09d' % recnum,' ',tield_contents.tag,'  ',' L',tield_contents.value())
        ## end loop 2

An example for the output:

python pymarc_000002.py

...

000000001   001     L 002547390
000000001   003     L OCoLC
000000001   005     L 20181016125657.0
000000001   008     L 180214t20182018enka     b    001 0 eng
000000001   092     L 302 BER m
000000001   020     L 9781138093317
000000001   035     L (OCoLC)991673448
000000001   040     L eng rda
000000001   041     L eng
000000001   100     L Bereczkei, Tamás lat author
000000001   245     L Machiavellianism : the psychology of manipulation / Tamas Bereczkei.
000000001   264     L London : Routledge, 2018.
000000001   264     L ©2018

Upvotes: 2

Views: 690

Answers (1)

swandog
swandog

Reputation: 749

If the marc field has a first or second indicator, then it will be parsed into .indicator1 or .indicator2 attributes. They will be both be defined even if only one is defined in the marc record, so something like this should work.

if hasattr(field_contents, 'indicator1'):
       indicator1 = field_contents.indicator1
       indicator2 = field_contents.indicator2

Upvotes: 0

Related Questions