如何获取 'pybtex.utils.OrderedCaseInsensitiveDict' 的字典键
How to get the dictionary keys of a 'pybtex.utils.OrderedCaseInsensitiveDict'
我正在尝试将中文提供文件转换为 CSV 文件。我试过直接使用 pandas 像 :
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), Beck Hopelessness Scale (BHS), 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', 1), (u'Dos', 2), (u'Tres', 3)])
>>> list(d.keys())
[u'Uno', u'Dos', u'Tres']
>>> list(d.items())
[(u'Uno', 1), (u'Dos', 2), (u'Tres', 3)]
>>> list(d.values())
[1, 2, 3]
我正在尝试将中文提供文件转换为 CSV 文件。我试过直接使用 pandas 像 :
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), Beck Hopelessness Scale (BHS), 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', 1), (u'Dos', 2), (u'Tres', 3)])
>>> list(d.keys())
[u'Uno', u'Dos', u'Tres']
>>> list(d.items())
[(u'Uno', 1), (u'Dos', 2), (u'Tres', 3)]
>>> list(d.values())
[1, 2, 3]