2012-11-22 6 views
5

pythonを使用して引用キーに基づいてbibtexファイルから特定のエントリを削除するにはどうすればよいですか?私は基本的に2つの引数(bibtexファイルと引用キーへのパス)をとり、そのキーに対応するエントリをファイルから削除する関数が必要です。私は正規表現で遊んだが成功しなかった。私もbibtexパーサのために少し見えましたが、それは過剰なもののようです。下のスケルトン関数では、決定的な部分はcontent_modified =です。ここでPythonを使用して引用キーに基づいてbibtexファイルから特定のエントリを削除する

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]:] 
+0

行頭で '}'を閉じることに依存しているので、中括弧を数えて項目の終わりを知る必要がありますか? –

答えて

1

ベニCherniavsky-Paskinコメントで述べたように、あなたはあなたのBibTeXエントリは右(任意のタブまたはスペース)せず、ラインの開始後に開始および終了すること、という事実に依存する必要があります。次に、これを行うことができます:

pattern = re.compile(r"^@\w+\{"+key+r",.*?^\}", re.S | re.M) 
content_modified = re.sub(pattern, "", content) 

2つの修飾子に注意してください。 Sは、改行改行を.にします。 Mは、文字列の先頭で^と一致します。

この事実に頼ることができないのであれば、BibTex形式は普通の言語ではありません(正確な結果を得るためには{}のネストが許されていますので、正規表現のフレーバーがあります。 (再帰またはバランシンググループを使用して)、私はPythonがそれらの機能のどれもサポートしていないと考えていますので、実際にはBibTexパーサーを使用する必要があります(コードをもっと控えめにしてしまうでしょう)。

+0

ありがとう!私はこれを試してみます。 – user2503795

関連する問題