Skip to content

Commit

Permalink
init commit
Browse files Browse the repository at this point in the history
  • Loading branch information
tao-bai committed Nov 21, 2021
1 parent 4fc4c92 commit a3cd61f
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,12 @@
# bib-combiner
# bib-combiner

A small python tool for combining multiple .bib files and removing duplicates by their IDs (ID: the string used for citation in lalex.)

## Usage
```bash
python bib_combiner.py bib1.bib bib2.bib bib3.bib .bib
```
output is named `final.bib`.

## Requirements
see `requiremetns.txt` generated by [pipreqs](https://pypi.org/project/pipreqs/).
35 changes: 35 additions & 0 deletions bib_combiner.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import os
import sys
import bibtexparser

from bibtexparser.bwriter import BibTexWriter
from bibtexparser.bibdatabase import BibDatabase

if __name__ == "__main__":
bibtex_list = sys.argv[1:]
print(bibtex_list)

db_final = BibDatabase()
db_final.entries = []

bib_ids = []
bib_total = 0
bib_sum = 0
duplicate_num = 0

for bibtex in bibtex_list:
with open(bibtex, 'r') as bibtex_file:
bib_database = bibtexparser.load(bibtex_file)
for bib_dict in bib_database.entries:
bib_total += 1
if bib_dict['ID'] not in bib_ids:
bib_sum += 1
bib_ids.append(bib_dict['ID'])
db_final.entries.append(bib_dict)
else:
duplicate_num += 1

print(f'Total: {bib_total}, Duplicate:{duplicate_num}, Final:{bib_sum}')

with open('final.bib', 'w') as bibtex_file:
bibtexparser.dump(db_final, bibtex_file)
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bibtexparser==1.2.0
python==3.9

0 comments on commit a3cd61f

Please sign in to comment.