forked from rctatman/bib_to_csv
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbib_to_csv.py
55 lines (48 loc) · 1.61 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
47
48
49
50
51
52
53
54
55
from pybtex.database.input import bibtex
import string
#open a bibtex file
parser = bibtex.Parser()
bibdata = parser.parse_file("/Users/mcewen/AP/Docs/Bibtex/mybibs_new.bib")
# tralsator for removing punctuation
translator = str.maketrans('', '', string.punctuation)
# open our output file
f = open('mybibs_new.csv', 'w')
# header row
f.write("Title, Year, Authors, Journal, Arxiv\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
title = b["title"]
title = title.replace('{', '')
title = title.replace('}', '')
f.write('"' + title + '"')
f.write(" ,")
f.write(b["year"])
f.write(" ,")
#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)
new_author = new_author.replace('{', '')
new_author = new_author.replace('}', '')
if len(authors) == 0:
authors = '"' + new_author
else:
authors = authors + ", " + new_author
f.write(authors + '"')
f.write(" ,")
if 'eprint' in b.keys():
f.write(b["eprint"])
f.write(" ,")
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()