Skip to content

Instantly share code, notes, and snippets.

@aewallin
Created October 9, 2022 07:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aewallin/a06b4be296d6b284144ff50273710782 to your computer and use it in GitHub Desktop.
Save aewallin/a06b4be296d6b284144ff50273710782 to your computer and use it in GitHub Desktop.
Modify bibtex file for new JabRef, old timestamp-fields modified to new creationdate-fields (is8601 format)
# -*- coding: utf-8 -*-
"""
Created on Sun Oct 9 10:09:25 2022
@author: awanders
"""
import bibtexparser
import datetime
"""
existing bibtex file has time-stamps in the format:
timestamp = {2021.05.07 09:45},
replace these with new format:
creationdate = {2022-10-05T16:51:46},
"""
bibfile = 'IonClock.bib'
with open(bibfile, encoding="utf8") as bibtex_file:
bib_database = bibtexparser.load(bibtex_file)
#%%
nmod=0
for e in bib_database.entries:
#print(e['timestamp'], len(e['timestamp'] ) )
try:
if len(e['timestamp'] ) == 10:
ts = datetime.datetime.strptime( e['timestamp'] , "%Y.%m.%d")
else:
ts = datetime.datetime.strptime( e['timestamp'] , "%Y.%m.%d %H:%M") # read old timestamp
e['creationdate'] = ts.isoformat(timespec='seconds') # write new timestamp
nmod = nmod+1
print("new", e['creationdate'])
except:
pass
print('Total entries: ', len(bib_database.entries))
print('Modified entries: ', nmod)
# Total entries: 2650
# Modified entries: 2639
#%% write modified database to file
writer = bibtexparser.bwriter.BibTexWriter()
with open('modified.bib', 'w', encoding="utf8") as bibfile2:
bibfile2.write(writer.write(bib_database))
#print(bib_database.entries)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment