Wie kann ein bestimmter Eintrag aus einer Bibtex-Datei basierend auf einem Cite-Schlüssel mit Python gelöscht werden? Ich möchte im Grunde eine Funktion, die zwei Argumente (Pfad zu bibtex Datei und Cite Key) und löscht den Eintrag, der dem Schlüssel aus der Datei entspricht. Ich habe mit regulären Ausdrücken gespielt, war aber nicht erfolgreich. Ich habe auch nach Bibtex-Parsern gesucht, aber das scheint ein Overkill zu sein. In der folgenden Skelettfunktion ist der entscheidende Teil content_modified =
.Löschen eines bestimmten Eintrags aus einer Bibtex-Datei basierend auf Cite-Schlüssel mit Python
def deleteEntry(path, key):
# get content of bibtex file
f = open(path, 'r')
content = f.read()
f.close()
# delete entry from content string
content_modified =
# rewrite file
f = open(path, 'w')
f.write(content_modified)
f.close()
Hier ist ein Beispiel bibtex-Datei (mit Leerzeichen in der Zusammenfassung):
@article{dai2008thebigfishlittlepond,
title = {The {Big-Fish-Little-Pond} Effect: What Do We Know and Where Do We Go from Here?},
volume = {20},
shorttitle = {The {Big-Fish-Little-Pond} Effect},
url = {http://dx.doi.org/10.1007/s10648-008-9071-x},
doi = {10.1007/s10648-008-9071-x},
abstract = {The big-fish-little-pond effect {(BFLPE)} refers to the theoretical prediction that equally able students will have lower academic
self-concepts in higher-achieving or selective schools or programs than in lower-achieving or less selective schools or programs,
largely due to social comparison based on local norms. While negative consequences of being in a more competitive educational
setting are highlighted by the {BFLPE}, the exact nature of the {BFLPE} has not been closely scrutinized. This article provides
a critique of the {BFLPE} in terms of its conceptualization, methodology, and practical implications. Our main argument is that
of the {BFLPE.}},
number = {3},
journal = {Educational Psychology Review},
author = {Dai, David Yun and Rinn, Anne N.},
year = {2008},
keywords = {education, composition by performance, education, peer effect, education, school context, education, social comparison/big-fish{\textendash}little-pond effect},
pages = {283--317},
file = {Dai_Rinn_2008_The Big-Fish-Little-Pond Effect.pdf:/Users/jpl2136/Documents/Literatur/Dai_Rinn_2008_The Big-Fish-Little-Pond Effect.pdf:application/pdf}
}
@book{coleman1966equality,
title = {Equality of Educational Opportunity},
shorttitle = {Equality of educational opportunity},
publisher = {{U.S.} Dept. of Health, Education, and Welfare, Office of Education},
author = {Coleman, James},
year = {1966},
keywords = {\_task\_obtain, education, school context, soz. Ungleichheit, education}
}
EDIT: Hier ist eine Lösung, die ich mit aufkommen. Es basiert nicht auf der Übereinstimmung mit dem gesamten Bibtex-Eintrag, sondern sucht stattdessen nach allen Anfängen und entfernt dann den entsprechenden Eintrag durch Schneiden der Kontext-Zeichenfolge.
content_keys = [(m.group(1), m.start(0)) for m in re.finditer("@\w{1,20}\{([\w\d-]+),", content)]
idx = [k[0] for k in content_keys].index(key)
content_modified = content[0:content_keys[idx][1]] + content[content_keys[idx + 1][1]:]
Können Sie Verlassen Sie sich darauf, '}' am Anfang der Zeile zu schließen, müssen Sie Klammern zählen, um zu wissen, wo ein Eintrag endet? –