Save XML in custom order of attribute in Python [duplicate]
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
This question already has an answer here:
Preserving XML attribute order?
1 answer
I am trying to get specific order of XML attribute.
Given
<OKV s="*****" r="*****" a="*****" g="****" m="*****" e="****" d="****" i="****" n="****" v="1"/>
Required :
<OKV v="1" n="****" i="****" d="****" e="****" m="*****" g="****" a="*****" r="*****" s="*****"/>
I am trying to implement in Python.
So far when I am using xml library.
import xml.etree.ElementTree as ET
tree = ET.parse('adhar.xml')
But it keeps read in that format,
Please tell how to change the order.
As I am reading correctly in internet explorer when opened the file, But getting reversed order while reading the file or see it in Chrome.
python xml sorting encoding
marked as duplicate by kjhughes
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 4 at 15:58
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
Preserving XML attribute order?
1 answer
I am trying to get specific order of XML attribute.
Given
<OKV s="*****" r="*****" a="*****" g="****" m="*****" e="****" d="****" i="****" n="****" v="1"/>
Required :
<OKV v="1" n="****" i="****" d="****" e="****" m="*****" g="****" a="*****" r="*****" s="*****"/>
I am trying to implement in Python.
So far when I am using xml library.
import xml.etree.ElementTree as ET
tree = ET.parse('adhar.xml')
But it keeps read in that format,
Please tell how to change the order.
As I am reading correctly in internet explorer when opened the file, But getting reversed order while reading the file or see it in Chrome.
python xml sorting encoding
marked as duplicate by kjhughes
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 4 at 15:58
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
Preserving XML attribute order?
1 answer
I am trying to get specific order of XML attribute.
Given
<OKV s="*****" r="*****" a="*****" g="****" m="*****" e="****" d="****" i="****" n="****" v="1"/>
Required :
<OKV v="1" n="****" i="****" d="****" e="****" m="*****" g="****" a="*****" r="*****" s="*****"/>
I am trying to implement in Python.
So far when I am using xml library.
import xml.etree.ElementTree as ET
tree = ET.parse('adhar.xml')
But it keeps read in that format,
Please tell how to change the order.
As I am reading correctly in internet explorer when opened the file, But getting reversed order while reading the file or see it in Chrome.
python xml sorting encoding
This question already has an answer here:
Preserving XML attribute order?
1 answer
I am trying to get specific order of XML attribute.
Given
<OKV s="*****" r="*****" a="*****" g="****" m="*****" e="****" d="****" i="****" n="****" v="1"/>
Required :
<OKV v="1" n="****" i="****" d="****" e="****" m="*****" g="****" a="*****" r="*****" s="*****"/>
I am trying to implement in Python.
So far when I am using xml library.
import xml.etree.ElementTree as ET
tree = ET.parse('adhar.xml')
But it keeps read in that format,
Please tell how to change the order.
As I am reading correctly in internet explorer when opened the file, But getting reversed order while reading the file or see it in Chrome.
This question already has an answer here:
Preserving XML attribute order?
1 answer
python xml sorting encoding
python xml sorting encoding
asked Jan 4 at 6:56
Chinmay DasChinmay Das
929
929
marked as duplicate by kjhughes
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 4 at 15:58
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by kjhughes
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 4 at 15:58
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
For this specific xml string you could use xmltodict (install with pip install xmltodict). This converts the xml data to a python dictionary. Using the following would reverse the order of the OKV entry:
import xmltodict
import collections
xml_data = '<OKV s="*****" r="*****" a="*****" g="****" m="*****" e="****" d="****" i="****" n="****" v="1"/>'
xml_data_parsed = xmltodict.parse(xml_data)
xml_data_parsed["OKV"] = collections.OrderedDict(reversed(list(xml_data_parsed["OKV"].items())))
xml_data = xmltodict.unparse(xml_data_parsed)
If the the part you want to reorder is embedded in a larger xml document you would have to select the deeper nested key to get to e.g. OKV.
add a comment |
Try using lxml to parse the xml file. As suggested here, it might help get the attributes in the appropriate order.
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
For this specific xml string you could use xmltodict (install with pip install xmltodict). This converts the xml data to a python dictionary. Using the following would reverse the order of the OKV entry:
import xmltodict
import collections
xml_data = '<OKV s="*****" r="*****" a="*****" g="****" m="*****" e="****" d="****" i="****" n="****" v="1"/>'
xml_data_parsed = xmltodict.parse(xml_data)
xml_data_parsed["OKV"] = collections.OrderedDict(reversed(list(xml_data_parsed["OKV"].items())))
xml_data = xmltodict.unparse(xml_data_parsed)
If the the part you want to reorder is embedded in a larger xml document you would have to select the deeper nested key to get to e.g. OKV.
add a comment |
For this specific xml string you could use xmltodict (install with pip install xmltodict). This converts the xml data to a python dictionary. Using the following would reverse the order of the OKV entry:
import xmltodict
import collections
xml_data = '<OKV s="*****" r="*****" a="*****" g="****" m="*****" e="****" d="****" i="****" n="****" v="1"/>'
xml_data_parsed = xmltodict.parse(xml_data)
xml_data_parsed["OKV"] = collections.OrderedDict(reversed(list(xml_data_parsed["OKV"].items())))
xml_data = xmltodict.unparse(xml_data_parsed)
If the the part you want to reorder is embedded in a larger xml document you would have to select the deeper nested key to get to e.g. OKV.
add a comment |
For this specific xml string you could use xmltodict (install with pip install xmltodict). This converts the xml data to a python dictionary. Using the following would reverse the order of the OKV entry:
import xmltodict
import collections
xml_data = '<OKV s="*****" r="*****" a="*****" g="****" m="*****" e="****" d="****" i="****" n="****" v="1"/>'
xml_data_parsed = xmltodict.parse(xml_data)
xml_data_parsed["OKV"] = collections.OrderedDict(reversed(list(xml_data_parsed["OKV"].items())))
xml_data = xmltodict.unparse(xml_data_parsed)
If the the part you want to reorder is embedded in a larger xml document you would have to select the deeper nested key to get to e.g. OKV.
For this specific xml string you could use xmltodict (install with pip install xmltodict). This converts the xml data to a python dictionary. Using the following would reverse the order of the OKV entry:
import xmltodict
import collections
xml_data = '<OKV s="*****" r="*****" a="*****" g="****" m="*****" e="****" d="****" i="****" n="****" v="1"/>'
xml_data_parsed = xmltodict.parse(xml_data)
xml_data_parsed["OKV"] = collections.OrderedDict(reversed(list(xml_data_parsed["OKV"].items())))
xml_data = xmltodict.unparse(xml_data_parsed)
If the the part you want to reorder is embedded in a larger xml document you would have to select the deeper nested key to get to e.g. OKV.
edited Jan 4 at 9:38
answered Jan 4 at 7:34
AscurionAscurion
36519
36519
add a comment |
add a comment |
Try using lxml to parse the xml file. As suggested here, it might help get the attributes in the appropriate order.
add a comment |
Try using lxml to parse the xml file. As suggested here, it might help get the attributes in the appropriate order.
add a comment |
Try using lxml to parse the xml file. As suggested here, it might help get the attributes in the appropriate order.
Try using lxml to parse the xml file. As suggested here, it might help get the attributes in the appropriate order.
answered Jan 4 at 7:03
BabokBabok
466
466
add a comment |
add a comment |