How to update field with bibtexparser ?

258 views Asked by At

I want to read a .bib file (Which I would be downloading frequently) and then read all the entries in it and wherever a predefined set of fields inside the entry is missing, add the specific field with some static information and then update the file.

For example, If I have a file input.bib like this:

@article{ ISI:000361215300002,
Abstract = {{some abstract}},
Year = {{2016}},
Volume = {{47}}
}

Then I would execute something like this:

python code.py < input.bib > input.bib

And inside the code.py, I want to do something like:

def populateKey(data):
for entry in data.entries:
    if 'key' not in entry:
        entry['key'] = KEY_VALUE

bib_str = ""
for line in sys.stdin:
    bib_str += line

bib_data = loads(bib_str)
populateKey(bib_data)
bibtex_str = bibtexparser.dumps(bib_data)
print bibtex_str

After I execute above code, I am getting an output that looks like:

@article{ ISI:000361215300002,
abstract = {some abstract},
key = value
volume = {47},
year = {2016},
}

The bibtex module is corrupting my format in that it makes everything lowercase and removes redundant brackets and jumbles the fields. Is there a way to not overwrite the file and just add a specific field wherever the specific field is not present?

0

There are 0 answers