2012-11-22 17 views
5

का उपयोग करके साइट कुंजी पर आधारित एक बिटबिक्स फ़ाइल से एक विशिष्ट प्रविष्टि को हटाकर पाइथन का उपयोग कर एक साइट कुंजी के आधार पर एक बिटबिक्स फ़ाइल से एक विशिष्ट प्रविष्टि को कैसे हटाया जा सकता है? मैं मूल रूप से एक फ़ंक्शन चाहता हूं जो दो तर्क (बिबटेक्स फ़ाइल और साइट कुंजी के लिए पथ) लेता है और फ़ाइल से कुंजी के अनुरूप प्रविष्टि को हटा देता है। मैंने नियमित अभिव्यक्तियों के साथ खेला लेकिन सफल नहीं हुआ। मैंने बिबटेक्स पार्सर्स के लिए थोड़ा सा देखा लेकिन यह एक ओवरकिल की तरह लगता है। नीचे कंकाल समारोह में, निर्णायक हिस्सा content_modified = है।पायथन

@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} 
} 

संपादित करें:: यहाँ एक समाधान है कि मैं के साथ आया है

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, की तलाश करता है और फिर संदर्भ स्ट्रिंग को स्लाइस करके संबंधित प्रविष्टि को हटा देता है।

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) 

दो संशोधक नोट करें। S. मैच लाइन ब्रेक बनाता है। स्ट्रिंग की शुरुआत में M^ मैच बनाता है।

यदि आप इस तथ्य पर भरोसा नहीं कर सकते हैं, तो बिबटेक्स प्रारूप बस एक नियमित भाषा नहीं है (क्योंकि यह {} के घोंसले की अनुमति देता है जिसे सही परिणामों के लिए गिना जाना चाहिए। रेगेक्स स्वाद हैं, जो अभी भी यह कार्य संभव कर सकते हैं (रिकर्सन या बैलेंसिंग ग्रुप का उपयोग करके), लेकिन मुझे लगता है कि पायथन उन सुविधाओं में से कोई भी समर्थन नहीं करता है। इसलिए, आपको वास्तव में एक बिबटेक्स पार्सर का उपयोग करना होगा (जो आपके कोड को और अधिक समझने योग्य बनाता है, मुझे लगता है)।

+0

धन्यवाद! मैं इसे आज़माउंगा। मैंने प्रश्न के लिए अपना दृष्टिकोण भी जोड़ा। – user2503795