1

I am working with the pymarc library. My question is, how to I deal with multiple instances of a variable, either building a list, or otherwise iterating through them?

A MARC field can be accessed by adding the field number to the record variable. For instance, I have three instances of an 856 field in one record, which can be accessed as record['856']. But only the first instance is passed.

assigning a variable record['856'][0] or record['856'][1] etc, doesn't work.

I have tried creating a list, which is shown below, but that didn't work

from pymarc import MARCReader

with open('file.mrc', 'rb') as fh:
    reader = MARCReader(fh)
    for record in reader: 
            """get all 856 fields  -- delete unwanted 856 fields
            =856  40$uhttp://url1.org
            =856  40$uhttp://url2.org
            =856  40$uhttp://url3.org
            """
                eight56to956s = []
                eight56to956 = record['856']
                eight56to956s.append(eight56to956)
                print eight56to956s

I know how I would do this in php, but I'm not getting my head around python syntax to even search the web for the right thing.

4

1 回答 1

1

您需要一个字典,您可以在其中将 856 设置为键,然后将要标记为 856 的值列表

your_856 = {856: ['=856  40$uhttp://url1.org', '=856  40$uhttp://url2.org', '=856  40$uhttp://url3.org']}

您现在可以使用 访问这些值index,这是一个示例

print(your_856[856][1])

这输出

=856  40$uhttp://url2.org
于 2016-03-04T17:23:52.517 回答