2012-12-11 23 views
2

我已經創建了一個腳本,可以爲抽象和關鍵字刮掉很多pdf。我還收集了一些bibtex文件,其中我想放置我提取的文本。我正在尋找的是向bibtex文件添加元素的一種方法。將元素添加到Python中的bibtex文件中

我寫了一個簡短的解析器:

#!/usr/bin/python 
#-*- coding: utf-8 

import os 
from pybtex.database.input import bibtex 

dir_path = "nime_archive/nime/bibtex/" 
num_texts = 0 

class Bibfile: 
    def __init__(self,bibs): 
    self.bibs = bibs 
    for a in self.bibs.entries.keys(): 
     num_text += 1 
     print bibs.entries[a].fields['title'] 
     #Need to implement a way of getting just the nime-identificator 
     try: 
     print bibs.entries[a].fields['url'] 
     except: 
     print "couldn't find URL for text: %s " % a 


    print "creating new bibfile" 



bibfiles = [] 
parser = bibtex.Parser() 


for infile in os.listdir(dir_path): 
    if infile.endswith(".bib"): 
     print infile 
     bibfiles = Bibfile(parser.parse_file(dir_path+infile)) 

我的問題是,如果有可能使用Pybtex將元素添加到現有中文提供檔案(或創建一個副本),所以我可以合併我拔牙什麼是可用的。如果在Pybtex中這是不可能的,我可以使用其他bibtex解析器?

+1

您需要正確縮進代碼。在Python中,這是**不是可選**。 'num_text + = 1'沒有正確縮進;除此之外,請儘量在整個代碼中保留一個4位的縮進值,並且不要改變它(就像您在最後幾行所做的那樣) – ThiefMaster

+0

感謝您提供的語法提示。我使用TextMate,並嘗試保持一致的語法,但是當我複製,移動並改變它時,它有時會變得有點不一致,特別是因爲我無法直觀地看到它是製表符還是空格,或者沒有提示if該塊有三個或四個深度縮進。有沒有一種好的清潔方法,並以這種方式使代碼更加正確? – olovholm

+0

還有Python風格指南,又名[PEP 8](http://www.python.org/dev/peps/pep-0008/)。最好是全部閱讀,但至少要閱讀[indentation](http://www.python.org/dev/peps/pep-0008/#indentation)上的部分(非常簡短的總結:總是用四個空格的倍數縮進;從不使用製表符,一個好的文本編輯器會自動轉換這個)。 – Evert

回答

1

我從來沒有使用pybtex,但從快速瀏覽一下,你可以添加條目。由於self.bibs.entries看起來是dict,您可以想出一個唯一的密鑰,並向其添加更多條目。例如:

key = "some_unique_string" 
new_entry = Entry('article', 
     fields={ 
      'language': u'english', 
      'title': u'Predicting the Diffusion Coefficient in Supercritical Fluids', 
      'journal': u'Ind. Eng. Chem. Res.', 
      'volume': u'36', 
      'year': u'1997', 
      'pages': u'888-895', 
     }, 
     persons={'author': [Person(u'Liu, Hongquin'), Person(u'Ruckenstein, Eli')]}, 
    ) 
self.bibs.entries[key] = new_entry 

(警告:未經測試)

如果你想知道在那裏我得到這個例子形式:必須在tests/子目錄pybtex源的外觀。上面的代碼示例主要來自tests/database_test/data.py。如果缺乏實際的文檔,測試可以成爲文檔的一個很好的來源。

+0

感謝您的評論,這看起來像我一直在尋找的東西。我仍然陷於一個問題。我已經有了書目數據,但是我想將它們與抽象和關鍵詞合併在一起。是否有任何Python結構可以用來訪問元素,然後添加所需的部分? – olovholm

+0

這裏是我嘗試執行的代碼:https://github.com/olovholm/NIME/blob/master/bibtexparser.py我提出的錯誤狀態:錯誤:'Entry'對象不支持項目分配(第48和49行) – olovholm

+0

dir(bibtex.Entry)= ['__class__','__delattr__','__dict__','__doc__','__eq__','__format__','__getattribute__','__hash__','__init__' ,'__module__','__new__','__reduce__','__reduce_ex__','__repr__','__setattr__','__sizeof__','__str__','__subclasshook__','__weakref__','add_person','get_crossref'] – olovholm

0

.data.add_entry(key,entry)適用於我。在這裏我使用了一個手動創建的條目(來自Evert的示例),但是您可以複製另一個也在解析的圍脖的現有條目。

from pybtex.database.input.bibtex import Parser 
    from pybtex.core import Entry, Person 

    key = "some_unique_string" 

    new_entry = Entry('article', 
      fields={ 
       'language': u'english', 
       'title': u'Predicting the Diffusion Coefficient in Supercritical Fluids', 
       'journal': u'Ind. Eng. Chem. Res.', 
       'volume': u'36', 
       'year': u'1997', 
       'pages': u'888-895', 
      }, 
    persons={'author': [Person(u'Liu, Hongquin'), Person(u'Ruckenstein, Eli')]}, 
     ) 

    newbib_parser = Parser() 
    newbib_parser.data.add_entry(key, new_entry) 
    print newbib_parser.data