实用程序类包装lxml,用于将mods v3.4xml元数据中的数据读入python数据类型。

pymods的Python项目详细描述


pymods是用于处理国会图书馆mods的实用模块。 xml标准:元数据描述模式(mods)。它是一个公用事业 特定于从中反序列化数据的lxml模块的包装器 将modsxml转换为python数据类型。

如果需要一个模块将数据序列化为modsxml,请参阅另一个模块 pymods by Matt Cordial

安装

推荐:

pip install pymods

使用

基础知识

XML使用modsreader类进行解析:

mods_records = pymods.MODSReader('some_file.xml')

单个记录存储为modsrecord对象的迭代器:

In[5]:forrecordinmods_records:....:print(record)....:<Element{http://www.loc.gov/mods/v3}modsat0x47a69f8><Element{http://www.loc.gov/mods/v3}modsat0x47fd908><Element{http://www.loc.gov/mods/v3}modsat0x47fda48>

modsreader将使用mods:modsCollection文档,输出 从OAI-PMH提要,或单个modsxml文档中使用mods:mods 作为根元素。

pymods.modsrecord

modsreader类将每个mods:mods元素解析为 pymods.modsrecord对象。modsrecord是一个自定义包装类 对于lxml.elementBase类。pymods.record的所有子级继承 lxml.u元素和lxml.elementbase方法。

In[6]:record=next(pymods.MODSReader('example.xml'))In[7]:print(record.nsmap){'dcterms':'http://purl.org/dc/terms/','xsi':'http://www.w3.org/2001/XMLSchema-instance',None:'http://www.loc.gov/mods/v3','flvc':'info:flvc/manifest/v1','xlink':'http://www.w3.org/1999/xlink','mods':'http://www.loc.gov/mods/v3'}
In[8]:forchildinrecord.iterdescendants():....:print(child.tag){http://www.loc.gov/mods/v3}identifier{http://www.loc.gov/mods/v3}extension{info:flvc/manifest/v1}flvc{info:flvc/manifest/v1}owningInstitution{info:flvc/manifest/v1}submittingInstitution{http://www.loc.gov/mods/v3}titleInfo{http://www.loc.gov/mods/v3}title{http://www.loc.gov/mods/v3}name{http://www.loc.gov/mods/v3}namePart{http://www.loc.gov/mods/v3}role{http://www.loc.gov/mods/v3}roleTerm{http://www.loc.gov/mods/v3}roleTerm{http://www.loc.gov/mods/v3}typeOfResource{http://www.loc.gov/mods/v3}genre...

方法

所有函数都以字符串、列表、命名列表的形式返回数据 元组。有关详细信息,请参阅相应的docstring。

>>>record.genre?Type:propertyStringform:<propertyobjectat0x0000000004812C78>Docstring:Accessesmods:genreelement.:return:AlistcontainingGenreelementswithterm,authority,authorityURI,andvalueURIattributes.

示例

导入

frompymodsimportMODSReader,MODSRecord

分析文件

In[10]:mods=MODSReader('example.xml')In[11]:forrecordinmods:....:print(record.dates)....:[Date(text='1966-12-08',type='{http://www.loc.gov/mods/v3}dateCreated')]None[Date(text='1987-02',type='{http://www.loc.gov/mods/v3}dateIssued')]

简单任务

生成标题列表

In[14]:forrecordinmods:....:print(record.titles)....:['Fire Line System']['$93,668.90. One Mill Tax Apportioned by Various Ways Proposed']['Broward NOW News: National Organization for Women, February 1987']

创建主题列表

In[17]:forrecordinmods:....:forsubjectinrecord.subjects:....:print(subject.text)....:ConcerthallsArchitectureArchitecturaldrawingsStructuralsystemsStructuralsystemsdrawingsStructuraldrawingsSafetyequipmentConstructionMechanicsStructuraloptimizationArchitecturaldesignFireprevention--SafetymeasuresTaxesTaxpayersTaxcollectionOrganizationsFeminismSexdiscriminationagainstwomenWomen's rightsEqualrightsamendmentsWomen--SocietiesandclubsNationalOrganizationforWomen

更复杂的任务

仅为lcsh主题创建主题uri的列表

In[18]:forrecordinmods:....:forsubjectinrecord.subjects:....:if'lcsh'==subject.authority:....:print(subject.uri)....:http://id.loc.gov/authorities/subjects/sh85082767http://id.loc.gov/authorities/subjects/sh88004614http://id.loc.gov/authorities/subjects/sh85132810http://id.loc.gov/authorities/subjects/sh85147343

使用无版权的us rightsstatement.org uri获取对象的url

In[23]:forrecordinmods:....:forrights_eleminrecord.rights....:ifrights_elem.uri=='http://rightsstatements.org/vocab/NoC-US/1.0/':....:print(record.purl)....:http://purl.flvc.org/fsu/fd/FSU_MSS0204_B01_F10_09http://purl.flvc.org/fsu/fd/FSU_MSS2008003_B18_F01_004

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java如何反射地迭代数组字段?   java NamedQuery错误“具有给定标识符的多行:1”   java无法使用单独类中的计时器更新TextView   兼容性什么时候可以很快使用新的Java功能?   java二叉树路径和   java矩形的性能   java我想从同一个子表在主表中添加两个外键   java如何获取基于特定日期的所有数据?   java javafx、OO编程规则和写入变量类型的选择   java使用带枚举的switch语句   java异步任务生成运行时异常   java为什么JLabel不显示下划线字符?   java如何解析具有可变参数号的函数?   带有按钮的java JavaFX自定义列表单元格:未调用处理程序   java Modelmapper无法映射整个模型?   传递给持久化的java分离实体,包含LatLng列表