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

情到浓时终转凉″ 提交于 2021-01-29 05:40:20

问题


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

回答1:


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.



来源:https://stackoverflow.com/questions/64972354/how-can-i-get-pybtex-to-read-an-absent-field-as-null-instead-of-skipping-the-rec

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!