With the help of some plugin, I get a .bib file with information about scientific articles. Sometimes it turns out that the same keys appear in different records.
For example:
JavaScript
x
20
20
1
@inproceedings{Hosseini_2016,
2
doi = {10.1109/ism.2016.0028},
3
url = {https://doi.org/10.1109%2Fism.2016.0028},
4
year = 2016,
5
month = {dec},
6
publisher = {{IEEE}},
7
author = {Mohammad Hosseini and Viswanathan Swaminathan},
8
title = {Adaptive 360 {VR} Video Streaming: Divide and Conquer},
9
booktitle = {2016 {IEEE} International Symposium on Multimedia ({ISM})}
10
}
11
@inproceedings{Hosseini_2016,
12
doi = {10.1109/ism.2016.0093},
13
url = {https://doi.org/10.1109%2Fism.2016.0093},
14
year = 2016,
15
month = {dec},
16
publisher = {{IEEE}},
17
author = {Mohammad Hosseini and Viswanathan Swaminathan},
18
title = {Adaptive 360 {VR} Video Streaming Based on {MPEG}-{DASH} {SRD}},
19
booktitle = {2016 {IEEE} International Symposium on Multimedia ({ISM})}
20
I am using pybtex library to parse a file. This library ignores duplicate entries with the same keys. Before using this library, I need to somehow process the file so that all the keys in it are different. How can I do that?
Advertisement
Answer
I decided to use regular expressions. There is probably a more convenient solution. I just replace the keys with nanoid.
JavaScript
1
11
11
1
from nanoid import generate
2
3
def process_bibtex(fn):
4
with open(fn, encoding="utf-8") as r_file:
5
bibtex = r_file.read()
6
pattern = r"@([wW]+?){([wW0-9_-]+?),"
7
def callback(matchobj):
8
return f"@{matchobj.group(1)}{{{generate()},"
9
with open(fn, "w", encoding="utf-8") as w_file:
10
w_file.write(re.sub(pattern, callback, bibtex))
11