-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbib_to_csv.py
46 lines (40 loc) · 1.28 KB
/
bib_to_csv.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
from pybtex.database.input import bibtex
import string
#open a bibtex file
parser = bibtex.Parser()
bibdata = parser.parse_file("bibliography.bib")
# tralsator for removing punctuation
translator = str.maketrans('', '', string.punctuation)
# open our output file
f = open('my_bib.csv', 'w')
# header row
f.write("title\t year\t author\t journal\n")
#loop through the individual references
for bib_id in bibdata.entries:
b = bibdata.entries[bib_id].fields
try:
# change these lines to create a SQL insert
f.write(b["title"])
f.write(" \t")
f.write(b["year"])
f.write(" \t")
#deal with multiple authors
authors = ""
for author in bibdata.entries[bib_id].persons["author"]:
new_author = str(author.first()) + " " + str(author.last())
new_author = new_author.translate(translator)
if len(authors) == 0:
authors = '"' + new_author
else:
authors = authors + ", " + new_author
f.write(authors + '"')
f.write(" \t")
f.write(b["journal"])
# field may not exist for a reference
except(KeyError):
f.write(b["booktitle"])
f.write("\n")
continue
f.write("\n")
# close output file
f.close()