从bibtex文件中提取注释组(在Python中?)

2024-09-29 23:23:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试扩展一点BibDesk中用于管理组的特性集,我想从一个程序中操作bibtex注释,BibDesk在其中写下静态组的信息。你知道吗

要做到这一点,我需要一个系统和强大的方式,以获得所有这是在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来实现它,但是pythonic的方法是什么呢? 我最拿手的就是一个本土的解析器

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

Tags: keyinstringiflinegroupcommentstatic
1条回答
网友
1楼 · 发布于 2024-09-29 23:23:13

这是对sed命令的快速而肮脏的翻译。不过,我不一定推荐这种方法,因为它不是特别健壮。你知道吗

import re

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

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

相关问题 更多 >

    热门问题