2017-10-29 2 views
0

BibDesk에서 그룹을 관리하는 데 사용할 수있는 기능 세트를 약간 확장하려고합니다. BibDesk에서 Static에 대한 정보를 적어주는 bibtex 주석을 프로그램에서 조작하고 싶습니다. 여러 떼.파이썬에서 bibtex 파일의 주석 그룹을 추출합니다.

이렇게하려면 bibtex 파일의이 주석 부분 안에있는 모든 것을 얻으려면 체계적이고 강력한 방법이 필요합니다.

@comment{BibDesk Static Groups{ 
<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> 
<plist version="1.0"> 
<array> 
    <dict> 
     <key>group name</key> 
     <string>MyGroupName</string> 
     <key>keys</key> 
     <string>BitexRefId1,BitexRefId2</string> 
    </dict> 
</array> 
</plist> 
}} 

나는 내가 그것으로 무엇을 해야할지 생각하는 XML array에 내 손을 넣어되면,하지만 첫 번째 부분은 @comment{BibDesk Static Groups{을 얻는 것은 나에게 조금 까다 롭습니다. sed을 사용하는 방법을 알고 싶습니다. sed -e '/@comment{BibDesk Static Groups{/,/}/!d' test.bib을 사용하고 있습니다.하지만이를 수행하는 비법은 무엇입니까? 내 가장 좋은 것은 기본적으로 집에서 성장 파서

file = open(file_name,"r") 
for line in file: 
    if static_groups_group: 
     if "}" in line: 
      static_groups_group=False 
      print "ending static group block" 
    if static_groups_group: 
     xml_groups.append(line) 
    if "@comment{BibDesk Static Groups{" in line: 
     print line," found" 
     static_groups_group=True 
+0

이 라이브러리는 다음을 수행 할 수 있습니다. https://bibtexparser.readthedocs.io/ –

답변

0

이것은 당신의 sed 명령의 신속하고 더러운 번역 한 것입니다이었다. 특히 강력하지는 않기 때문에 필자는이 접근 방식을 반드시 추천하지는 않습니다.

import re 

with open(file_name) as fp: 
    text = fp.read() 

groups = re.findall(r'\@comment\{BibDesk Static Groups\{(.*?)\}\}', text, re.DOTALL) 
관련 문제