微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何获取“pybtex.utils.OrderedCaseInsensitiveDict”的字典键

如何解决如何获取“pybtex.utils.OrderedCaseInsensitiveDict”的字典键

我正在尝试将 bibtex 文件制作成 CSV 文件。我试过直接使用熊猫:

with open('bibTex.bib') as bibtex_file:
  bib_database = bibtexparser.load(bibtex_file)
df = pd.DataFrame(bib_database.entries)
df.to_csv('ref.csv',index=False)

但是由于我的 , 文件中有很多 .bib,它无法按我的需要工作,所以我是这样做的:

import pandas as pd
import bibtexparser
from pybtex.database.input import bibtex
#open a bibtex file
parser = bibtex.Parser()
bibdata = parser.parse_file("bibTex.bib")

#loop through the individual references
for bib_id in bibdata.entries:
    b = bibdata.entries[bib_id].fields
    print(type(b))

它返回一个 <class 'pybtex.utils.OrderedCaseInsensitiveDict'> 我想获取一个通用 python 字典的键,但它不起作用我在文档中找不到任何关于它的内容

这段代码输出如下:

OrderedCaseInsensitiveDict([('title','{The Beck Depression Inventory-II (BDI-II),Beck Hopelessness Scale (BHS),and Beck Scale for Suicide Ideation (BSS).}'),('year','2004'),('month',''),('journal',('booktitle','Comprehensive handbook of psychological assessment,Vol. 2: Personality assessment.'),('publisher','John Wiley & Sons,Inc.'),('address','Hoboken,NJ,US'),('volume',('number',('pages','50--69'),('doi',('isbn','0-471-41612-6 (Hardcover)'),('issn',('url',('note',('abstract',"This chapter describes the Beck Depression Inventory-Second edition (BDI-II),and Beck Scale for Suicide Ideation (BSS). Given that the BDI-II is the most widely used of these measures,coupled with the fact that comprehensive reviews of this revised instrument have yet to appear in the literature,the primary focus of this chapter concerns the examination of the BDI-II. However,the remaining scales that we review are used widely as well,especially in the assessment of depression. Although we do not review the Beck Anxiety Inventory (BAI),which is another of the most commonly used Beck scales,readers are directed to some recent review papers (see Steer & Beck,1997; Wilson,de Beurs,Palmer,& Chambless,1999). We begin with a review of the principal features,test development,psychometric characteristics,research status,and applicability of each of these instruments. We also discuss the limitations of these measures,mention age and cross-cultural factors,highlight accommodations made for persons with disabilities,address legal and ethical issues,and summarize each instrument's current research status. Following this examination,we underscore how these measures may be used in clinical practice. (PsycInfo Database Record (c) 2021 APA,all rights reserved)"),('comment',('eprint',('groups',('keywords','*Beck Depression Inventory,*Hopelessness,*rating Scales,*Suicidal Ideation,Psychometrics,Test Construction'),('owner',('printed',('priority',('qualityassured',('ranking',('readstatus',('relevance',('timestamp','')])

非常感谢:)

解决方法

0.23.0 版本开始,OrderedCaseInsensitiveDict 已使用 collections.OrderedDict 实现 所以要获取键和值,您可以使用 .keys() 和 .values() 参见下面的 example

>>>import pybtex.utils as p 
>>>d = p.OrderedCaseInsensitiveDict([
    ...     ('Uno',1),...     ('Dos',2),...     ('Tres',3),... ])
>>> d
OrderedCaseInsensitiveDict([(u'Uno',(u'Dos',(u'Tres',3)])
>>> list(d.keys())
[u'Uno',u'Dos',u'Tres']
>>> list(d.items())
[(u'Uno',3)]
>>> list(d.values())
[1,2,3] 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?