-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbibclean.py
31 lines (25 loc) · 1.01 KB
/
bibclean.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
def remove_unused_references(aux_path, bib_path, out_path):
with open(aux_path, 'r') as f:
aux_contents = f.read()
used_citations = set()
for line in aux_contents.split('\n'):
if line.startswith("\\citation{"):
used_citations.update(line[len("\\citation{"):-1].split(','))
with open(bib_path, 'r') as f:
bib_contents = f.read()
new_bib_contents = ''
record = None
for line in bib_contents.split('\n'):
if line.startswith("@"):
if record is not None and citation_key in used_citations:
new_bib_contents += record
record = line + '\n'
citation_key = line.split('{', 1)[1].split(',', 1)[0]
elif record is not None:
record += line + '\n'
if record is not None and citation_key in used_citations:
new_bib_contents += record
with open(out_path, 'w') as f:
f.write(new_bib_contents)
# 使用方法:
remove_unused_references('new0403.aux', 'old.bib', 'new.bib')