Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/352.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
有没有用Python编写的GEDCOM解析器?_Python_Parsing_Genealogy_Gedcom - Fatal编程技术网

有没有用Python编写的GEDCOM解析器?

有没有用Python编写的GEDCOM解析器?,python,parsing,genealogy,gedcom,Python,Parsing,Genealogy,Gedcom,GEDCOM是交换家谱数据的标准 我找到了用英文写的解析器 甚至 但到目前为止,还没有一个是用Python编写的。我得到的最接近的文件是来自GRAMPS项目的文件,但是它充满了对GRAMPS模块的引用,以至于我无法使用它 我只想要一个用Python编写的简单独立的GEDCOM解析器库。这是否存在?您可以使用该工具通过本机语言接口包含C库。您必须在Python中对C api进行调用,但其余代码只能是Python 听起来可能有点吓人,但一旦你设置好了东西,将两者结合使用也不会有什么坏处。根

GEDCOM是交换家谱数据的标准

我找到了用英文写的解析器

  • 甚至
但到目前为止,还没有一个是用Python编写的。我得到的最接近的文件是来自GRAMPS项目的文件,但是它充满了对GRAMPS模块的引用,以至于我无法使用它

我只想要一个用Python编写的简单独立的GEDCOM解析器库。这是否存在?

您可以使用该工具通过本机语言接口包含C库。您必须在Python中对C api进行调用,但其余代码只能是Python


听起来可能有点吓人,但一旦你设置好了东西,将两者结合使用也不会有什么坏处。根据C库是如何编写的,可能会有一些怪癖,但无论您使用哪种选项,都必须处理一些怪癖。

几年前,我用Python编写了一个简单的GEDCOM到XML转换器,作为。我发现以XML格式处理GEDCOM数据要容易得多(特别是当下一步涉及XSLT时)

我目前还没有在线代码,所以我已经将模块粘贴到了这条消息中。这对我有用;没有保证。希望这能有所帮助

import codecs, os, re, sys
from xml.sax.saxutils import escape

fn = sys.argv[1]

ged = codecs.open(fn, encoding="cp437")
xml = codecs.open(fn+".xml", "w", "utf8")
xml.write("""<?xml version="1.0"?>\n""")
xml.write("<gedcom>")
sub = []
for s in ged:
    s = s.strip()
    m = re.match(r"(\d+) (@(\w+)@ )?(\w+)( (.*))?", s)
    if m is None:
        print "Error: unmatched line:", s
    level = int(m.group(1))
    id = m.group(3)
    tag = m.group(4)
    data = m.group(6)
    while len(sub) > level:
        xml.write("</%s>\n" % (sub[-1]))
        sub.pop()
    if level != len(sub):
        print "Error: unexpected level:", s
    sub += [tag]
    if id is not None:
        xml.write("<%s id=\"%s\">" % (tag, id))
    else:
        xml.write("<%s>" % (tag))
    if data is not None:
        m = re.match(r"@(\w+)@", data)
        if m:
            xml.write(m.group(1))
        elif tag == "NAME":
            m = re.match(r"(.*?)/(.*?)/$", data)
            if m:
                xml.write("<forename>%s</forename><surname>%s</surname>" % (escape(m.group(1).strip()), escape(m.group(2))))
            else:
                xml.write(escape(data))
        elif tag == "DATE":
            m = re.match(r"(((\d+)?\s+)?(\w+)?\s+)?(\d{3,})", data)
            if m:
                if m.group(3) is not None:
                    xml.write("<day>%s</day><month>%s</month><year>%s</year>" % (m.group(3), m.group(4), m.group(5)))
                elif m.group(4) is not None:
                    xml.write("<month>%s</month><year>%s</year>" % (m.group(4), m.group(5)))
                else:
                    xml.write("<year>%s</year>" % m.group(5))
            else:
                xml.write(escape(data))
        else:
            xml.write(escape(data))
while len(sub) > 0:
    xml.write("</%s>" % sub[-1])
    sub.pop()
xml.write("</gedcom>\n")
ged.close()
xml.close()
导入编解码器、操作系统、re、系统
从xml.sax.saxutils导入转义
fn=sys.argv[1]
ged=codecs.open(fn,encoding=“cp437”)
xml=codecs.open(fn+“.xml”、“w”、“utf8”)
xml.write(“\n”)
xml.write(“”)
sub=[]
对于ged中的s:
s=s.条带()
m=re.match(r“(\d+)(@(\w+)@)(\w+(.*)?”,s)
如果m为无:
打印“错误:行不匹配:”,s
级别=整数(m组(1))
id=m.group(3)
标记=m.组(4)
数据=m组(6)
而len(sub)>级别:
write(“\n”%(子[-1]))
副主席()
如果级别为!=len(sub):
打印“错误:意外级别:”,s
sub+=[标签]
如果id不是无:
write(“%(标记,id))
其他:
write(“%(标记))
如果数据不是无:
m=重新匹配(r“@(\w+@)”,数据)
如果m:
xml.write(m.group(1))
elif标记==“名称”:
m=re.match(r“(.*?/(.*?/$”,数据)
如果m:
write(“%s%s”%”(转义(m.group(1.strip()),转义(m.group(2)))
其他:
write(转义(数据))
elif标记==“日期”:
m=re.match(r“((\d+)\s+(\w+)\s+)(\d{3,})”,数据)
如果m:
如果m.group(3)不是无:
write(“%s%s%s”%(m.group(3)、m.group(4)、m.group(5)))
elif m.group(4)不是无:
write(“%s%s”%”(m.group(4),m.group(5)))
其他:
write(“%s”%m.group(5))
其他:
write(转义(数据))
其他:
write(转义(数据))
当len(sub)>0时:
write(“%sub[-1])
副主席()
xml.write(“\n”)
ged.close()
xml.close()

Python中的通用GEDCOM解析器链接自

我从mwhite的答案中提取了代码,对其进行了一点扩展(好的,不仅仅是一点),并发布在github:。我对其他要添加的内容提出了建议:-)

我知道这个帖子很旧,但我在搜索和这个项目中都找到了它


源代码非常干净且功能强大。

GEDCOM 5.5格式的另一个基本解析器:

或使用ctypes或Cython(源自Pyrex)。请不要在明显的主题外问题上发布答案!离题问题可以关闭和删除,这可能会使您的贡献无效。在这里,问题是需要一个非现场资源,并且正在接近结束。