读取、写入和修改MARC书目数据
项目描述
_|_|_| _| _| _|_|_| _|_| _|_|_| _| _|_| _|_|_|
_| _| _| _| _| _| _| _| _| _|_| _|
_| _| _| _| _| _| _| _| _| _| _|
_|_|_| _|_|_| _| _| _| _|_|_| _| _|_|_|
_| _|
_| _|_|
pymarc 是一个用于处理以 MARC21 编码的书目数据的 Python 库。它提供了一个用于读取、写入和修改 MARC 记录的 API。它主要被设计为一个应急弹出座椅,用于将数据资产从 MARC 中提取出来,并以某种更合理的表示形式。然而,多年来它被用来创建和修改 MARC 记录,尽管有 反复的呼吁 让其死亡作为格式,但 MARC 似乎仍然作为一个僵尸般幸福地生活着。
以下是您可能想要使用 pymarc 的常见示例。如果您认为有示例应该在这里,请发送拉取请求。
您可以在这里阅读 pymarc 文档。
安装
您可能只想使用 pip 来安装 pymarc
pip install pymarc
如果您想下载并安装最新源代码,您需要 git
git clone git://gitlab.com/pymarc/pymarc.git
您还需要 setuptools。一旦您有了源代码和 setuptools,运行 pymarc 测试套件以确保分发的正确性
python setup.py test
然后进行安装
python setup.py install
读取
通常情况下,您将拥有一些MARC数据,并希望从中提取数据。以下是一个读取一批记录并打印标题的示例。如果您感兴趣,这个示例使用的是pymarc存储库中可用的批次文件。
from pymarc import MARCReader
with open('test/marc.dat', 'rb') as fh:
reader = MARCReader(fh)
for record in reader:
print(record.title)
The pragmatic programmer : from journeyman to master /
Programming Python /
Learning Python /
Python cookbook /
Python programming for the absolute beginner /
Web programming : techniques for integrating Python, Linux, Apache, and MySQL /
Python programming on Win32 /
Python programming : an introduction to computer science /
Python Web programming /
Core python programming /
Python and Tkinter programming /
Game programming with Python, Lua, and Ruby /
Python programming patterns /
Python programming with the Java class libraries : a tutorial for building Web
and Enterprise applications /
Learn to program using Python : a tutorial for hobbyists, self-starters, and all
who want to learn the art of computer programming /
Programming with Python /
BSD Sockets programming from a multi-language perspective /
Design patterns : elements of reusable object-oriented software /
Introduction to algorithms /
ANSI Common Lisp /
pymarc.Record
对象有一些方便的属性,例如title
,用于获取文献记录的片段,其他属性包括:author
、isbn
、subjects
、location
、notes
、physicaldescription
、publisher
、pubyear
、issn
、issn_title
。但是,实际上,要处理MARC数据,您需要了解用于指定各种信息的数字字段标签和子字段代码。在MARC记录中隐藏着比这些辅助属性提供访问的数据多得多的数据。例如,title
属性通过提取来自245
字段的信息来工作,在幕后使用子字段a
和b
。您可以像这样访问245a
:
print(record['245']['a'])
某些字段,如主题,可以重复。在这种情况下,您将希望使用get_fields
来获取所有这些字段作为pymarc.Field
对象,然后您可以进一步与之交互。
for f in record.get_fields('650'):
print(f)
如果您是MARC字段的初学者,理解MARC是一个相当不错的入门指南,一旦您了解了基础知识,图书馆的MARC 21格式页面也是一个很好的参考。
编写
注意:从v5.0.0版本开始,使用Subfield
来创建子字段。在v5之前,子字段作为字符串列表构建和访问,例如:[code, value, code, value]
。在v5.0.0中,这已经改为将子字段组织成一个元组的列表,例如:[(code, value), (code, value)]
。《Subfield》作为《NamedTuple》实现,因此元组可以构建为Subfield(code=code, value=value)
。不再支持创建子字段的旧风格。尝试将字符串列表传递给Field
构造函数的subfields
参数将引发ValueError
。为了方便起见,可以使用Field.convert_legacy_subfields
类方法将旧列表转换为Subfield
列表。
以下是一个创建记录并将其写入文件的示例。
from pymarc import Record, Field, Subfield, Indicators
record = Record()
record.add_field(
Field(
tag='245',
indicators=Indicators('0', '1'),
subfields=[
Subfield(code='a', value='The pragmatic programmer : '),
Subfield(code='b', value='from journeyman to master /'),
Subfield(code='c', value='Andrew Hunt, David Thomas.')
]))
with open('file.dat', 'wb') as out:
out.write(record.as_marc())
要将旧字符串列表转换为Subfield
列表,可以在Field
类上提供.convert_legacy_subfields
类方法。
from pymarc import Field, Subfield
legacy_fields: list[str] = ['a', 'The pragmatic programmer : ',
'b', 'from journeyman to master /',
'c', 'Andrew Hunt, David Thomas']
coded_fields: list[Subfield] = Field.convert_legacy_subfields(legacy_fields)
更新
更新工作方式相同,您读取它,修改它,然后再次写入。
from pymarc import MARCReader
with open('test/marc.dat', 'rb') as fh:
reader = MARCReader(fh)
record = next(reader)
record['245']['a'] = 'The Zombie Programmer : '
with open('file.dat', 'wb') as out:
out.write(record.as_marc())
JSON和XML
如果您发现自己在相当多的情况下使用MARC数据并分发它,通过使用JSON或XML序列化,您可能会让其他开发者更加快乐。使用XML或JSON的主要好处是使用UTF8字符编码,而不是令人沮丧的古老的MARC8编码。此外,他们可以使用标准的JSON和XML读取/写入工具来获取他们想要的数据,而不是像pymarc这样的疯狂MARC处理库。
XML
要解析MARCXML记录的文件,您可以
from pymarc import parse_xml_to_array
records = parse_xml_to_array('test/batch.xml')
如果您有一个大XML文件,并且不想将它们全部读入内存,您可以
from pymarc import map_xml
def print_title(r):
print(r.title)
map_xml(print_title, 'test/batch.xml')
此外,如果您愿意,除了传递到map_xml和parse_xml_to_array的路径外,还可以传递一个文件对象。
from pymarc import parse_xml_to_array
records = parse_xml_to_array(open('test/batch.xml'))
JSON
JSON支持相对较少,您可以调用pymarc.Record
的as_json()
方法来返回给定MARC记录的JSON。
from pymarc import MARCReader
with open('test/one.dat','rb') as fh:
reader = MARCReader(fh)
for record in reader:
print(record.as_json(indent=2))
{
"leader": "01060cam 22002894a 4500",
"fields": [
{
"001": "11778504"
},
{
"010": {
"ind1": " ",
"subfields": [
{
"a": " 99043581 "
}
],
"ind2": " "
}
},
{
"100": {
"ind1": "1",
"subfields": [
{
"a": "Hunt, Andrew,"
},
{
"d": "1964-"
}
],
"ind2": " "
}
},
{
"245": {
"ind1": "1",
"subfields": [
{
"a": "The pragmatic programmer :"
},
{
"b": "from journeyman to master /"
},
{
"c": "Andrew Hunt, David Thomas."
}
],
"ind2": "4"
}
},
{
"260": {
"ind1": " ",
"subfields": [
{
"a": "Reading, Mass :"
},
{
"b": "Addison-Wesley,"
},
{
"c": "2000."
}
],
"ind2": " "
}
},
{
"300": {
"ind1": " ",
"subfields": [
{
"a": "xxiv, 321 p. ;"
},
{
"c": "24 cm."
}
],
"ind2": " "
}
},
{
"504": {
"ind1": " ",
"subfields": [
{
"a": "Includes bibliographical references."
}
],
"ind2": " "
}
},
{
"650": {
"ind1": " ",
"subfields": [
{
"a": "Computer programming."
}
],
"ind2": "0"
}
},
{
"700": {
"ind1": "1",
"subfields": [
{
"a": "Thomas, David,"
},
{
"d": "1956-"
}
],
"ind2": " "
}
}
]
}
如果您想解析MARCJSON记录的文件,您可以
from pymarc import parse_json_to_array
records = parse_json_to_array(open('test/batch.json'))
print(records[0])
=LDR 00925njm 22002777a 4500
=001 5637241
=003 DLC
=005 19920826084036.0
=007 sdubumennmplu
=008 910926s1957\\\\nyuuun\\\\\\\\\\\\\\eng\\
=010 \\$a 91758335
=028 00$a1259$bAtlantic
=040 \\$aDLC$cDLC
=050 00$aAtlantic 1259
=245 04$aThe Great Ray Charles$h[sound recording].
=260 \\$aNew York, N.Y. :$bAtlantic,$c[1957?]
=300 \\$a1 sound disc :$banalog, 33 1/3 rpm ;$c12 in.
=511 0\$aRay Charles, piano & celeste.
=505 0\$aThe Ray -- My melancholy baby -- Black coffee -- There's no you -- Doodlin' -- Sweet sixteen bars -- I surrender dear -- Undecided.
=500 \\$aBrief record.
=650 \0$aJazz$y1951-1960.
=650 \0$aPiano with jazz ensemble.
=700 1\$aCharles, Ray,$d1930-$4prf
支持
PyMARC 开发者鼓励您加入 PyMARC Google Group,如果您需要帮助。同时,请随时使用 GitLab 上的问题跟踪 提交功能请求或错误报告。如果您有什么痒点需要抓,请抓它,并通过 GitLab 发送合并请求。
如果您开始使用 MARC,您可能会觉得除了技术支持外,还需要道德支持。在 Libera 上的 #code4lib 频道是两者的好地方。
项目详情
下载文件
下载适合您平台的文件。如果您不确定选择哪个,请了解有关 安装软件包 的更多信息。
源代码分布
构建分布
pymarc-5.2.2.tar.gz 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 0913b4167a4337190598914c3642c5ae4f0f64a26bc4316f380cc54f65d8a741 |
|
MD5 | c3cf8c08b2876698c88d60d4bf77afd5 |
|
BLAKE2b-256 | 2c85b8fceffd3f093fbaf867212a18004f0a4e0a25130e9f44bcfae888bd406d |
pymarc-5.2.2-py3-none-any.whl 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | d654ee1faae5d80845b7a44b27deb6ea9b5377994d16aeba5d3e19e7856d7d43 |
|
MD5 | c053a78acc1b5e4b982967d248b14ffa |
|
BLAKE2b-256 | 32410c1c5564d9c816a54c6f40b389794d94963fb182b42d446baf0e942a9ae1 |