Search code examples
pythonpybtex

How to get the dictionary keys of a 'pybtex.utils.OrderedCaseInsensitiveDict'


I'm trying to make a bibtex file into a CSV file. I've tried to use pandas directly like :

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)

But as I have many , in my .bib file it doesn't work as I needed, so I've did it this way:

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))

It's returning a <class 'pybtex.utils.OrderedCaseInsensitiveDict'> I would like to get the keys of a common python dictionary but it's not working I dindn't find any thing about it the documentation.

The output of this code is the following:

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', '')])

Thank's very much :)


Solution

  • From version 0.23.0 OrderedCaseInsensitiveDict has been implemented using collections.OrderedDict so to get the keys and values you can use .keys() and .values() see example below:

    >>>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]