cách xóa một mục nhập cụ thể khỏi tệp bibtex dựa trên khóa trích dẫn bằng python? Về cơ bản tôi muốn có một hàm nhận hai đối số (đường dẫn đến tệp bibtex và khóa trích dẫn) và xóa mục nhập tương ứng với khóa từ tệp. Tôi đã chơi với các biểu thức thông thường nhưng không thành công. Tôi cũng nhìn một chút cho các trình phân tích cú pháp bibtex nhưng điều đó có vẻ như là quá mức cần thiết. Trong hàm khung bên dưới, phần quyết định là content_modified =
.xóa mục nhập cụ thể khỏi tệp bibtex dựa trên khóa trích dẫn bằng cách sử dụng 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()
Dưới đây là một ví dụ tập tin BibTeX (với không gian một cách trừu tượng):
@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: Đây là một giải pháp mà tôi đã đưa ra. Nó không dựa trên việc kết hợp toàn bộ mục nhập bibtex mà thay vào đó, hãy tìm tất cả các khởi đầu @article{dai2008thebigfishlittlepond,
và sau đó loại bỏ mục nhập tương ứng bằng cách cắt chuỗi ngữ cảnh.
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]:]
dựa vào việc đóng '}' ở đầu dòng, chúng ta có phải đếm niềng răng để biết một mục nhập kết thúc ở đâu? –