Skip to main content

Simple python interface for reading MARC-XML

Project description

Marcalyx

PyPi Version Build Status

Simple python interface for reading MARC-XML

Installation

pip install marcalyx

Usage

Import the package

>>> import marcalyx

Marcalyx works on xml.etree.ElementTree.Element objects (or compatible), so you will need to parse the XML before passing it to a Marcalyx object. For instance, given a file containing a single <record>:

>>> import xml.etree.ElementTree as ET

or, using lxml:

>>> import lxml.etree as ET

then:

>>> tree = ET.parse('tests/xml/1027474578.xml')
>>> record_element = tree.getroot()
>>> marc = marcalyx.Record(record_element)

If the XML contains a <collection> of Records you would instead create a marcalyx.Collection object, from which you can get the records:

>>> coll = marcalyx.Collection(collection_element)
>>> marc = coll.records()[0]

Fields

Once you have a record, you can access the fields by tag (always use the 3-character string, for example, "001", "010", "100"):

>>> marc.field("245")
[245 10$aKindred /$cOctavia E. Butler.]

Or, more simply:

>>> marc["245"]
[245 10$aKindred /$cOctavia E. Butler.]

Fields are always returned as an array when accessed by tag (but not necessarily from the convenience methods, below). Fields are either a ControlField:

>>> type(m["008"][0])
<class 'marcalyx.marcalyx.ControlField'>

Or a DataField:

>>> type(m["245"][0])
<class 'marcalyx.marcalyx.DataField'>

All fields have a tag and a value:

>>> m["008"][0].tag
'008'
>>> m["245"][0].tag
'245'
>>> m["008"][0].value
'180306r20141979xxk    g      000 j eng d'
>>> m["245"][0].value
'Kindred / Octavia E. Butler.'

Data fields have their two "indicators":

>>> m["245"][0].ind1
'1'
>>> m["245"][0].ind2
'0'

And subfields, which can be accessed via the subfield() method, which returns an array:

>>> m["245"][0].subfield('a')
[$aKindred /]

As with fields, you can get subfields via subscript:

>>> m["245"][0]['a']
[$aKindred /]

You can use a tuple containing a field tag and subfield code as a subscript to get a flat list of all the subfields with that code (if any) of all the fields with that tag (if any):

>>> m[('650','a')]
[$aAfrican American women, $aTime travel]

value vs. str

The string representation of a field is formatted in the conventional way, showing the indicators and subfields:

>>> str(m["245"][0])
'245 10$aKindred /$cOctavia E. Butler.'

The value is formatted for display:

>>> m["245"][0].value
'Kindred / Octavia E. Butler.'

Subfields

Subfields have codes, values, and string representations:

>>> m["245"][0].subfield("a")[0].code
'a'
>>> m["245"][0].subfield("a")[0].value
'Kindred /'
>>> str(m["245"][0].subfield("a")[0])
'$aKindred /'

Convenience methods

There are several methods to make it easier to get single fields or categories of fields. mainEntry() will return whichever of the 1XX fields the record has (as a DataField, not an array):

>>> m.mainEntry()
100 1#$aButler, Octavia Estelle$d(1947-2006).$4aut

#titleStatement gets the 245 field (again, as a DataField and not an array):

>>> m.titleStatement()
245 10$aKindred /$cOctavia E. Butler.

There are also methods to get an array of each of the main categories of fields. Each of these returns an array of all the fields in the record of the given category:

>>> marc.controlFields() # 00X
>>> marc.codes()         # 01X-09X
>>> marc.titles()        # 20X-24X
>>> marc.edition()       # 25X-28X
>>> marc.description()   # 3XX
>>> marc.series()        # 4XX
>>> marc.notes()         # 5XX
>>> marc.subjects()      # 6XX
>>> marc.addedEntries()  # 70X-75X
>>> marc.linking()       # 76X-78X
>>> marc.seriesAdded()   # 80X-83X
>>> marc.holdings()      # 841-88X

Some common numbers have convenience methods:

> record.lccn       # 010$a, String or nil
> record.isbns      # 020$a, Array of Strings, or []
> record.issns      # 022$a, Array of Strings, or []

Leader

You can get the record leader:

>>> marc.leader
'00000cam a2200000Mi 4500'

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/seanredmond/marcalyx. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

License

The gem is available as open source under the terms of the MIT License.

Code of Conduct

Everyone interacting in the Lccnorm project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

marcalyx-1.0.2.tar.gz (6.4 kB view details)

Uploaded Source

Built Distribution

marcalyx-1.0.2-py2.py3-none-any.whl (5.4 kB view details)

Uploaded Python 2Python 3

File details

Details for the file marcalyx-1.0.2.tar.gz.

File metadata

  • Download URL: marcalyx-1.0.2.tar.gz
  • Upload date:
  • Size: 6.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.20.1 setuptools/40.8.0 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.7.2

File hashes

Hashes for marcalyx-1.0.2.tar.gz
Algorithm Hash digest
SHA256 fef670cc6e55ab75466d7b4198e0f2dc72746415d8101c24a10224161fc04671
MD5 485254d0bf881af41dd056718797d0ed
BLAKE2b-256 38ffdc4bc75528414569dc2e1ceb629c1b977b0f4420474b6c1b00ada7b629dd

See more details on using hashes here.

File details

Details for the file marcalyx-1.0.2-py2.py3-none-any.whl.

File metadata

  • Download URL: marcalyx-1.0.2-py2.py3-none-any.whl
  • Upload date:
  • Size: 5.4 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.20.1 setuptools/40.8.0 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.7.2

File hashes

Hashes for marcalyx-1.0.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 226086dda558bd2c15e071de92874e42b3bb06d31bc05c93689c3ec6147f5520
MD5 39563800371792603ead93e92b978217
BLAKE2b-256 19a59d0906d19468eaa19cc39a8b9f418848899d40a93318ec9484600366797d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page