实用程序类包装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 Spring框架服务单元测试   在Java中遍历hashmaps的hashmap以检索字符串值   如何使用CodeQL检查Java注释是否具有特定属性?   java为什么在Spring Boot中访问此资源而不是登录弹出窗口需要始终获得完全身份验证   处理将多集计数转换为列表的过程   java另一个线性布局,没有出现按钮   eclipse Java映像加载未显示在jar中   java Junit类无法加载基本测试类ApplicationContext   java如何在main中使用my getvalues()方法打印列表   java Sonar,S128:切换案例应该以无条件的“中断”语句结束,而不是继续   java从socket读取字符串错误连接重置错误   java使用新数据刷新任意图表饼图   java通过异步运行lambda访问方法参数   java错误的结果一旦我处理try and catch