How can I get pybtex to read an absent field as NULL instead of skipping the record?
Asked Answered
G

1

0

I have been using pybtex (using a modified version of this) to pass records from a .bib file into a .csv like so

from pybtex.database.input import bibtex
import csv

parser = bibtex.Parser()
bibdata = parser.parse_file("../../bib/small.bib")
 
# create csv file 
with open('smallbib.csv', mode ='w') as csv_file:
    fieldnames = ['DOI',
                  'number']
    writer = csv.DictWriter(csv_file, fieldnames=fieldnames, lineterminator = '\n')
    writer.writeheader()
    for bib_id in bibdata.entries:
        b = bibdata.entries[bib_id].fields
        try:
            writer.writerow({'DOI': b['DOI'], 
                             'number': b["number"],})
        except(KeyError):
            continue

However, if a field doesn't exist in the .bib file, this script simply ignores the entire record. How can I get my script to write NULL or a blank cell instead? Is it to do with my except(KeyError) statment?

sample input data

@Article{adeniran2016n,
  number    = {3},
  doi       = {10.1021/acs.chemmater.5b05020},
}

@Article{blankenship2017cigarette,
  doi       = {10.1039/C7EE02616A},
}
Gratuity answered 23/11, 2020 at 16:30 Comment(0)
G
0

It looks like a better option is to just use bibtexparser and pandas. It's much simpler!

import bibtexparser
import pandas as pd

with open("../../bib/small.bib") as bibtex_file:
    bib_database = bibtexparser.load(bibtex_file)
    
df = pd.DataFrame(bib_database.entries)
selection = df[['doi', 'number']]
selection.to_csv('temp3.csv', index=False)

Adapted from this answer.

Gratuity answered 24/11, 2020 at 11:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.