如何使用python根据引用 key 从bibtex文件中删除特定条目?我基本上想要一个带有两个参数(bibtex文件的路径和cite键)并从文件中删除与键相对应的条目的函数。我玩正则表达式,但没有成功。我也为bibtex解析器看了些,但这似乎是一个过大的杀伤力。在下面的框架函数中,决定性的部分是content_modified =
。
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()
这是一个示例bibtex文件(摘要中带有空格):
@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}
}
编辑:这是我想出的解决方案。它不是基于匹配整个bibtex条目,而是查找所有开头的
@article{dai2008thebigfishlittlepond,
,然后通过对上下文字符串进行切片来删除相应的条目。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]:]
最佳答案
正如Beni Cherniavsky-Paskin在评论中提到的那样,您将不得不依赖这样一个事实,即您的BibTex条目将在该行开始之后立即开始和结束(没有任何制表符或空格)。然后,您可以执行以下操作:
pattern = re.compile(r"^@\w+\{"+key+r",.*?^\}", re.S | re.M)
content_modified = re.sub(pattern, "", content)
注意这两个修饰符。
S
使.
匹配换行符。 M
使^
在字符串的开头匹配。如果您不能依靠这个事实,那么BibTex格式根本不是一种常规语言(因为它允许嵌套
{}
,而嵌套ojit_code才能计算出正确的结果。有regex风格,仍然可以使此任务成为可能(使用递归或平衡组),但我认为Python不支持这些功能,因此,您实际上必须使用BibTex解析器(我想这也会使您的代码更加不稳定)。关于python - 使用Python根据引用 key 从bibtex文件中删除特定条目,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13506155/