• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Python xml.xml_to_dict函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Python中sunpy.util.xml.xml_to_dict函数的典型用法代码示例。如果您正苦于以下问题:Python xml_to_dict函数的具体用法?Python xml_to_dict怎么用?Python xml_to_dict使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了xml_to_dict函数的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。

示例1: get_header

def get_header(filepath):
    """
    Reads the header form the file
    
    Parameters
    ----------
    filepath : string
        The file to be read
        
    Returns
    -------
    headers : list
        A list of headers read from the file
    """
    xmlstring = _read_xmlbox(filepath, "fits")
    pydict = xml_to_dict(xmlstring)["fits"]
    
    #Fix types
    for k, v in pydict.items():
        if v.isdigit():
            pydict[k] = int(v)
        elif _is_float(v):
            pydict[k] = float(v)
            
    # Remove newlines from comment
    if 'comment' in pydict:
        pydict['comment'] = pydict['comment'].replace("\n", "")
            
    return [FileHeader(pydict)]
开发者ID:quintusdias,项目名称:sunpy,代码行数:29,代码来源:jp2.py


示例2: get_header

def get_header(filepath):
    """
    Reads the header from the file

    Parameters
    ----------
    filepath : string
        The file to be read

    Returns
    -------
    headers : list
        A list of headers read from the file
    """
    jp2 = Jp2k(filepath)
    xml_box = [box for box in jp2.box if box.box_id == 'xml ']
    xmlstring = ET.tostring(xml_box[0].xml.find('fits'))
    pydict = xml_to_dict(xmlstring)["fits"]

    #Fix types
    for k, v in pydict.items():
        if v.isdigit():
            pydict[k] = int(v)
        elif _is_float(v):
            pydict[k] = float(v)

    # Remove newlines from comment
    if 'comment' in pydict:
        pydict['comment'] = pydict['comment'].replace("\n", "")

    return [FileHeader(pydict)]
开发者ID:jongyeob,项目名称:sunpy,代码行数:31,代码来源:jp2.py


示例3: get_jp2_header

    def get_jp2_header(self, date, observatory=None, instrument=None, detector=None, measurement=None, jp2_id=None):
        """
        Get the XML header embedded in a JPEG2000 image. Includes the FITS header as well as a section
        of Helioviewer-specific metadata.

        This uses `getJP2Header <https://api.helioviewer.org/docs/v2/#JPEG2000>`_ from the Helioviewer API.

        Parameters
        ----------
        date : `astropy.time.Time`, `str`
            A `~sunpy.time.parse_time` parsable string or `~astropy.time.Time`
            object for the desired date of the image
        observatory : `str`
            Observatory name
        instrument : `str`
            Instrument name
        measurement : `str`
            Measurement name
        detector : `str`
            Detector name
        jp2_id : `int`
            Unique JP2 image identifier.
            This can be used directly instead of using the previous parameters.

        Returns
        -------
        out : `dict`
            Returns a dictionary containing the header information of JPEG 2000 image.
            The returned dictionary may have either one or two keys: *fits* and *helioviewer*.

        Examples
        --------
        >>> from sunpy.net import helioviewer
        >>> hv = helioviewer.HelioviewerClient()  # doctest: +REMOTE_DATA
        >>> header = hv.get_jp2_header('2012/07/03', observatory='SDO',
        ...                            instrument='HMI', detector=None, measurement='continuum')  # doctest: +REMOTE_DATA
        >>> # The key 'fits' can be used to get the fits header information
        >>> fits_header = header['fits']  # doctest: +REMOTE_DATA
        >>> # The keys 'helioviewer' can be used to extract the helioviewer specific metadata.
        >>> helioviewer_meta_data = header['helioviewer']  # doctest: +REMOTE_DATA
        """
        if jp2_id is None:
            try:
                jp2_id = self.get_closest_image(date, observatory, instrument, detector, measurement)['id']
            except KeyError:
                raise KeyError("The values used for observatory, instrument, detector, measurement "
                               "do not correspond to a source_id. Please check the list using "
                               "HelioviewerClient.data_sources.")

        params = {
            "action": "getJP2Header",
            "id" : jp2_id,
        }

        responses = self._request(params)
        # Reads the output from HTTPResponse object and decodes it.
        responses = responses.read().decode('utf-8')
        return xml_to_dict(responses)['meta']
开发者ID:drewleonard42,项目名称:sunpy,代码行数:58,代码来源:helioviewer.py


示例4: test_xml_to_dict4

def test_xml_to_dict4():
    """
    should return dict of xml string
    with empty value if there are no inner elements
    """
    source_xml = "<outer></outer>"

    xml_dict = xml.xml_to_dict(source_xml)
    expected_dict = {u'outer': ''}

    assert xml_dict == expected_dict
开发者ID:Alex-Ian-Hamilton,项目名称:sunpy,代码行数:11,代码来源:test_xml.py


示例5: test_with_multiple_children_in_list

def test_with_multiple_children_in_list():
    """
    Setting the 'multiple' attribute of parent node should put child nodes in a
    list.
    """
    def getChild(lst_of_children, key, value):
        for child in lst_of_children:
            if child[key] == value:
                return child

        raise ValueError("No children with key {0} set to {1} found.".format(key, value))

    source = '''<?xml version="1.0" encoding="UTF-8"?>
                <Config>
                    <Name>With multiple children</Name>
                    <Children multiple="true">
                      <Child>
                        <Name>First Child</Name>
                        <Value>Value 1</Value>
                      </Child>
                      <Child>
                        <Name>Second Child</Name>
                        <Value>Value 2</Value>
                      </Child>
                    </Children>
                </Config>'''

    expected = {'Config': {'Children': [{'Name': 'First Child', 'Value': 'Value 1'},
                                        {'Name': 'Second Child', 'Value': 'Value 2'}],
                           'Name': 'With multiple children'}}

    actual = xml.xml_to_dict(source)

    assert len(expected['Config']) == len(actual['Config'])
    assert expected['Config']['Name'] == actual['Config']['Name']

    assert len(actual['Config']['Children']) == 2

    # As the child dictionaries are in lists we cannot be certain what order
    # they are in. Test individualy.

    expected_children = expected['Config']['Children']
    actual_children = actual['Config']['Children']

    expected_first_child = getChild(expected_children, key='Name', value='First Child')
    actual_first_child = getChild(actual_children, key='Name', value='First Child')

    assert expected_first_child == actual_first_child

    expected_second_child = getChild(expected_children, key='Name', value='Second Child')
    actual_second_child = getChild(actual_children, key='Name', value='Second Child')

    assert expected_second_child == actual_second_child
开发者ID:Cadair,项目名称:sunpy,代码行数:53,代码来源:test_xml.py


示例6: test_xml_to_dict1

def test_xml_to_dict1():
    """
    should return dict of xml string
    """
    source_xml = "<outer>\
          <inner1>one</inner1>\
          <inner2>two</inner2>\
        </outer>"

    xml_dict = xml.xml_to_dict(source_xml)
    expected_dict = {u'outer': {u'inner2': u'two', u'inner1': u'one'}}

    assert xml_dict == expected_dict
开发者ID:Alex-Ian-Hamilton,项目名称:sunpy,代码行数:13,代码来源:test_xml.py


示例7: get_header

def get_header(filepath):
    """Reads the header in and saves it as a dictionary"""
    xmlstring = read_xmlbox(filepath, "fits")
    pydict = xml_to_dict(xmlstring)["fits"]
    
    #Fix types
    for k, v in pydict.items():
        if v.isdigit():
            pydict[k] = int(v)
        elif is_float(v):
            pydict[k] = float(v)
            
    return pydict
开发者ID:jpjustiniano,项目名称:sunpy,代码行数:13,代码来源:jp2.py


示例8: test_xml_to_dict2

def test_xml_to_dict2():
    """
    should return dict of xml string
    and if a tag is duplicated it takes the last one.
    """
    source_xml = "<outer>\
                    <inner1>one-one</inner1>\
                    <inner1>one-two</inner1>\
                    <inner2>two-one</inner2>\
                    <inner2>two-two</inner2>\
                 </outer>"

    xml_dict = xml.xml_to_dict(source_xml)
    expected_dict = {u'outer': {u'inner2': u'two-two', u'inner1': u'one-two'}}

    assert xml_dict == expected_dict
开发者ID:Alex-Ian-Hamilton,项目名称:sunpy,代码行数:16,代码来源:test_xml.py


示例9: get_voevent

    def get_voevent(self, as_dict=True, base_url="http://www.lmsal.com/hek/her?"):
        """Retrieves the VOEvent object associated with a given event and
        returns it as either a Python dictionary or an XML string."""

        # Build URL
        params = {"cmd": "export-voevent", "cosec": 1, "ivorn": self["kb_archivid"]}
        url = base_url + urllib.parse.urlencode(params)

        # Query and read response
        response = urllib.request.urlopen(url).read()

        # Return a string or dict
        if as_dict:
            return xml_to_dict(response)
        else:
            return response
开发者ID:ZachWerginz,项目名称:sunpy,代码行数:16,代码来源:hek.py


示例10: get_header

def get_header(filepath):
    """Reads the header in and saves it as a dictionary"""
    xmlstring = read_xmlbox(filepath, "fits")
    pydict = xml_to_dict(xmlstring)["fits"]
    
    #Fix types
    for k, v in pydict.items():
        if v.isdigit():
            pydict[k] = int(v)
        elif is_float(v):
            pydict[k] = float(v)
            
    # Remove newlines from comment
    pydict['comment'] = pydict['comment'].replace("\n", "")
            
    return MapHeader(pydict)
开发者ID:jslhs,项目名称:sunpy,代码行数:16,代码来源:jp2.py


示例11: test_xml_to_dict5

def test_xml_to_dict5():
    """
    should return dict of xml string with 2 layer nesting.
    """
    source_xml = "<outer>\
                    <mid1>\
                        <inner1>one-one</inner1>\
                    </mid1>\
                    <mid2>\
                        <inner2>two-one</inner2>\
                    </mid2>\
                 </outer>"

    xml_dict = xml.xml_to_dict(source_xml)
    expected_dict = {u'outer': {u'mid2': {u'inner2': u'two-one'}, u'mid1': {u'inner1': u'one-one'}}}

    assert xml_dict == expected_dict
开发者ID:Cadair,项目名称:sunpy,代码行数:17,代码来源:test_xml.py


示例12: test_xml_to_dict6

def test_xml_to_dict6():
    """
    should return dict of xml string
    with 2 layer nesting and if a tag is duplicated it takes the last one.
    """
    source_xml = "<outer>\
                    <mid>\
                        <inner1>one-one</inner1>\
                    </mid>\
                    <mid>\
                        <inner2>two-one</inner2>\
                    </mid>\
                 </outer>"

    xml_dict = xml.xml_to_dict(source_xml)
    expected_dict = {u'outer': {u'mid': {u'inner2': u'two-one'}}}

    assert xml_dict == expected_dict
开发者ID:Alex-Ian-Hamilton,项目名称:sunpy,代码行数:18,代码来源:test_xml.py


示例13: test_xml_to_dict5

def test_xml_to_dict5():
    """
    should return dict of xml string
    with 2 layer nesting
    """
    source_xml = "<outer>\
                    <mid1>\
                        <inner1>one-one</inner1>\
                    </mid1>\
                    <mid2>\
                        <inner2>two-one</inner2>\
                    </mid2>\
                 </outer>"

    xml_dict = xml.xml_to_dict(source_xml)
    expected_dict = {u"outer": {u"mid2": {u"inner2": u"two-one"}, u"mid1": {u"inner1": u"one-one"}}}

    assert xml_dict == expected_dict
开发者ID:narolez571,项目名称:sunpy,代码行数:18,代码来源:test_xml.py



注:本文中的sunpy.util.xml.xml_to_dict函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Python super_sum.super_sum函数代码示例发布时间:2022-05-27
下一篇:
Python scraper.Scraper类代码示例发布时间:2022-05-27
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap