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},
}