在 python 中使用 lxml.objectify 检查和修复附加到节点的值

发布于 2024-11-25 17:07:28 字数 479 浏览 1 评论 0原文

我正在尝试修改 PyKML,它使用 lxml.objectify。通过跟踪节点追加,它会变成具有默认 str() 行为的字符串。我想捕获列表或元组的附加并将它们转换为适当位置的分隔行而不是“(xx.xxxx, yy.yyyy)”

from pykml.factory import GX_ElementMaker as GX

track = GX.Track(id='track_%d' % group_num )
for pt in group:
    when = datetime.datetime.utcfromtimestamp(pt['ts'])
    track.append( KML.when( when ) )  # WHEN?
for pt in group:
    track.append( GX.coord( (pt['x'],pt['y'])) )  # <-- trouble here

谢谢, -库尔特

I'm trying to modify PyKML, which uses lxml.objectify. With a track node append, this gets turned into a string with the default str() behavior. I'd like to catch appends of lists or tuples and convert them to proper place separated lines rather than '(xx.xxxx, yy.yyyy)'

from pykml.factory import GX_ElementMaker as GX

track = GX.Track(id='track_%d' % group_num )
for pt in group:
    when = datetime.datetime.utcfromtimestamp(pt['ts'])
    track.append( KML.when( when ) )  # WHEN?
for pt in group:
    track.append( GX.coord( (pt['x'],pt['y'])) )  # <-- trouble here

Thanks,
-kurt

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

長街聽風 2024-12-02 17:07:28

使用命名空间信息创建一个干净的 ElementMaker 类。然后创建一个子类,将节点名称作为方法。在该方法中,处理所有奇怪的情况。然后制作将进入该节点名称位置的字符串,并返回具有该节点名称的干净 ElementMaker 类的实例。

<一href="http://code.google.com/r/schwehr-pykml/source/browse/src/pykml/factory.py?spec=svn 05a10cef3fd3c430389e8aca1313a20da932e565&r=05a10cef3fd3c430389e8aca1313a20da932e565" rel="nofollow">http://code.google.com/r/schwehr-pykml/source/browse/src/pykml/factory.py?spec= svn05a10cef3fd3c430389e8aca1313a20da932e565&r=05a10cef3fd3c430389e8aca1313a20da932e565

def indexable_levels(args):
    #print 'args:',args
    levels = 0
    while True:
        if isinstance(args,str): break
        try:
            args = args[0]
            levels += 1
        except:
            break
    #print '   levels ->',levels
    return levels

# Create a factory object for the KML Google Extension namespace
_GX_ElementMakerSimple = objectify.ElementMaker(
    annotate=False,
    namespace=nsmap['gx'],
    nsmap={'gx': nsmap['gx']},
)

class _GX_ElementMaker (objectify.ElementMaker):
    'KML ElementMaker with overloads for custom text payloads like coordinates'
    def coord(self, *args):
        #print 'start coord: "%s"' % (str(args)), type(args), len(args)
        levels = indexable_levels(args)
        if levels == 1 and len(args) == 1:
            # This case is really redundant with the next
            assert isinstance(args[0],str)
            return _GX_ElementMakerSimple.coord(args[0])
        if levels == 1:
            return _GX_ElementMakerSimple.coord(' '.join([str(item) for item in args]))
        if levels == 2:
            # ((-121.583851, 37.386052),)
            assert(len(args)==1)
            return _GX_ElementMakerSimple.coord(' '.join([str(item) for item in args[0] ]))
        assert(False)

# Create a factory object for the KML Google Extension namespace
GX_ElementMaker = _GX_ElementMaker(
    annotate=False,
    namespace=nsmap['gx'],
    nsmap={'gx': nsmap['gx']},
)

Create a clean ElementMaker class with the namespace info. Then create a subclass with the node name as a method. In that method, handle all the odd cases. Then craft the string that would go in that node names place and return an instance of the clean ElementMaker class with that node name.

http://code.google.com/r/schwehr-pykml/source/browse/src/pykml/factory.py?spec=svn05a10cef3fd3c430389e8aca1313a20da932e565&r=05a10cef3fd3c430389e8aca1313a20da932e565

def indexable_levels(args):
    #print 'args:',args
    levels = 0
    while True:
        if isinstance(args,str): break
        try:
            args = args[0]
            levels += 1
        except:
            break
    #print '   levels ->',levels
    return levels

# Create a factory object for the KML Google Extension namespace
_GX_ElementMakerSimple = objectify.ElementMaker(
    annotate=False,
    namespace=nsmap['gx'],
    nsmap={'gx': nsmap['gx']},
)

class _GX_ElementMaker (objectify.ElementMaker):
    'KML ElementMaker with overloads for custom text payloads like coordinates'
    def coord(self, *args):
        #print 'start coord: "%s"' % (str(args)), type(args), len(args)
        levels = indexable_levels(args)
        if levels == 1 and len(args) == 1:
            # This case is really redundant with the next
            assert isinstance(args[0],str)
            return _GX_ElementMakerSimple.coord(args[0])
        if levels == 1:
            return _GX_ElementMakerSimple.coord(' '.join([str(item) for item in args]))
        if levels == 2:
            # ((-121.583851, 37.386052),)
            assert(len(args)==1)
            return _GX_ElementMakerSimple.coord(' '.join([str(item) for item in args[0] ]))
        assert(False)

# Create a factory object for the KML Google Extension namespace
GX_ElementMaker = _GX_ElementMaker(
    annotate=False,
    namespace=nsmap['gx'],
    nsmap={'gx': nsmap['gx']},
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文