本文整理汇总了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)]
示例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)]
示例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']
示例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
示例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
示例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
示例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
示例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
示例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
示例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)
示例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
示例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
示例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