在Python 3中使用RISparser时readris()出现问题

2024-06-12 04:11:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我第一次在Python3中使用RISparser,无法使用readris()函数输出书目详细信息

以下是我现在使用的更新脚本:

import os
from pprint import pprint
from RISparser import readris

filepath='C:\\Users\\mobarget\\Google Drive\\RIS_export_PolishNationalLibrary'

for f in os.listdir(filepath): # define filepath as directory containing iterable files
    print(f) # returns correct file names, e.g. Primo_RIS_Export.ris
    f_path=os.path.join(filepath, f)
    with open(f_path, 'r', encoding="utf-8") as bibliography_file:
        
        print(bibliography_file)
# OUTPUT: <_io.TextIOWrapper name='C:\\Users\\mobarget\\Google Drive\\RIS_export_PolishNationalLibrary\\Primo_RIS_Export(12).ris' mode='r' encoding='utf-8'>
    
        entries=readris(bibliography_file)
        print(entries)
        
# OUTPUT: <generator object Base.parse at 0x000001E2840E29A8>       
        for entry in entries:
            print(entry['id']) # select entry according to tag-key-mapping
            print(entry['primary_title']) # select entry according to tag-key-mapping

正如您在注释中所看到的,readris()返回一些生成器对象信息,但不返回文件的内容。我的错在哪里


Tags: pathfromimportosusersfileentriespprint
1条回答
网友
1楼 · 发布于 2024-06-12 04:11:52

我联系了GITHUB上的开发人员,发现RISparser包已经过时:

That's correct. RISparser was version 0.4 and below, and we renamed and rebranded to rispy in version 0.5. The API for rispy is much cleaner and mirrors the python json library from the stdlib, which many people are familiar with.

For more details, read the changelog: https://github.com/MrTango/rispy/blob/master/HISTORY.rst#v05-2020-02-21

此代码正在运行:

# Please note that rispy is the follow-up version of RISparser
# download and documentation: https://pypi.org/project/rispy/

import os
from pprint import pprint
import rispy

# define file path for local RIS files
filepath='C:\\Users\\mobarget\\Google Drive\\RIS_export_PolishNationalLibrary'

# define filepath as directory containing iterable files

for f in os.listdir(filepath): 
    print(f) # return file names, e.g. Primo_RIS_Export.ris
    f_path=os.path.join(filepath, f)
    with open(f_path, 'r', encoding="utf-8") as bibliography_file:      
        print(bibliography_file)
        
        entries=rispy.load(bibliography_file)
                  
        for entry in entries:
            print(entry['primary_title'])

相关问题 更多 >