Create xml with multiple root elements from txt file
Im confused to convert content from .xml
to .txt
. Already I've dealt with conversion txt to xml, but problem with xml to txt seems to be harder.
App should take all values from child element and assign them into a list. The problem is that xml file has multiple roots elements (know that is invalid format) F.e: sample.xml looks as follows:
<row>
<col>One</col>
<col>Two</col>
<col>Three</col>
</row>
<row>
<col>John</col>
<col>Mark</col>
<col>Elise</col>
</row>
As the result sample.txt should looks as follows:
One Two Three
John Mark Elise
During writing this post i came with an idea but it looks ugly (especially with deleting col tag from line). Do you have any better ideas to get a values from child element |col|
?
My code:
def convert_xml_to_txt(self):
words_list =
with open(self.filename) as fxml:
while True:
line = fxml.readline()
if line:
if line.startswith('<row'):
pass
else:
words_list.append(line[5:-7].strip()) #delete tags <col> from line
else:
self.generate_txt_file(words_list)
break
python xml
add a comment |
Im confused to convert content from .xml
to .txt
. Already I've dealt with conversion txt to xml, but problem with xml to txt seems to be harder.
App should take all values from child element and assign them into a list. The problem is that xml file has multiple roots elements (know that is invalid format) F.e: sample.xml looks as follows:
<row>
<col>One</col>
<col>Two</col>
<col>Three</col>
</row>
<row>
<col>John</col>
<col>Mark</col>
<col>Elise</col>
</row>
As the result sample.txt should looks as follows:
One Two Three
John Mark Elise
During writing this post i came with an idea but it looks ugly (especially with deleting col tag from line). Do you have any better ideas to get a values from child element |col|
?
My code:
def convert_xml_to_txt(self):
words_list =
with open(self.filename) as fxml:
while True:
line = fxml.readline()
if line:
if line.startswith('<row'):
pass
else:
words_list.append(line[5:-7].strip()) #delete tags <col> from line
else:
self.generate_txt_file(words_list)
break
python xml
1
Have you considered using the built inxml
library?
– Kendas
Nov 13 '18 at 11:25
@Kendas: why consider that? It cannot handle such malformed XML data: "junk after document element: line 7, column 0".
– usr2564301
Nov 13 '18 at 12:26
add a comment |
Im confused to convert content from .xml
to .txt
. Already I've dealt with conversion txt to xml, but problem with xml to txt seems to be harder.
App should take all values from child element and assign them into a list. The problem is that xml file has multiple roots elements (know that is invalid format) F.e: sample.xml looks as follows:
<row>
<col>One</col>
<col>Two</col>
<col>Three</col>
</row>
<row>
<col>John</col>
<col>Mark</col>
<col>Elise</col>
</row>
As the result sample.txt should looks as follows:
One Two Three
John Mark Elise
During writing this post i came with an idea but it looks ugly (especially with deleting col tag from line). Do you have any better ideas to get a values from child element |col|
?
My code:
def convert_xml_to_txt(self):
words_list =
with open(self.filename) as fxml:
while True:
line = fxml.readline()
if line:
if line.startswith('<row'):
pass
else:
words_list.append(line[5:-7].strip()) #delete tags <col> from line
else:
self.generate_txt_file(words_list)
break
python xml
Im confused to convert content from .xml
to .txt
. Already I've dealt with conversion txt to xml, but problem with xml to txt seems to be harder.
App should take all values from child element and assign them into a list. The problem is that xml file has multiple roots elements (know that is invalid format) F.e: sample.xml looks as follows:
<row>
<col>One</col>
<col>Two</col>
<col>Three</col>
</row>
<row>
<col>John</col>
<col>Mark</col>
<col>Elise</col>
</row>
As the result sample.txt should looks as follows:
One Two Three
John Mark Elise
During writing this post i came with an idea but it looks ugly (especially with deleting col tag from line). Do you have any better ideas to get a values from child element |col|
?
My code:
def convert_xml_to_txt(self):
words_list =
with open(self.filename) as fxml:
while True:
line = fxml.readline()
if line:
if line.startswith('<row'):
pass
else:
words_list.append(line[5:-7].strip()) #delete tags <col> from line
else:
self.generate_txt_file(words_list)
break
python xml
python xml
edited Nov 13 '18 at 12:23
Alex.U
1,1882921
1,1882921
asked Nov 13 '18 at 11:20
rad_hrad_h
112
112
1
Have you considered using the built inxml
library?
– Kendas
Nov 13 '18 at 11:25
@Kendas: why consider that? It cannot handle such malformed XML data: "junk after document element: line 7, column 0".
– usr2564301
Nov 13 '18 at 12:26
add a comment |
1
Have you considered using the built inxml
library?
– Kendas
Nov 13 '18 at 11:25
@Kendas: why consider that? It cannot handle such malformed XML data: "junk after document element: line 7, column 0".
– usr2564301
Nov 13 '18 at 12:26
1
1
Have you considered using the built in
xml
library?– Kendas
Nov 13 '18 at 11:25
Have you considered using the built in
xml
library?– Kendas
Nov 13 '18 at 11:25
@Kendas: why consider that? It cannot handle such malformed XML data: "junk after document element: line 7, column 0".
– usr2564301
Nov 13 '18 at 12:26
@Kendas: why consider that? It cannot handle such malformed XML data: "junk after document element: line 7, column 0".
– usr2564301
Nov 13 '18 at 12:26
add a comment |
1 Answer
1
active
oldest
votes
There is a couple of libraries that you can use:
one is untangle
and can be installed by pip
like pip install untangle
:
import untangle
obj = untangle.parse('path/to/file.xml')
or you can use xmltodict
helpful library:
import xmltodict
with open('path/to/file.xml') as fd:
doc = xmltodict.parse(fd.read())
For more information look at This link.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53279915%2fcreate-xml-with-multiple-root-elements-from-txt-file%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
There is a couple of libraries that you can use:
one is untangle
and can be installed by pip
like pip install untangle
:
import untangle
obj = untangle.parse('path/to/file.xml')
or you can use xmltodict
helpful library:
import xmltodict
with open('path/to/file.xml') as fd:
doc = xmltodict.parse(fd.read())
For more information look at This link.
add a comment |
There is a couple of libraries that you can use:
one is untangle
and can be installed by pip
like pip install untangle
:
import untangle
obj = untangle.parse('path/to/file.xml')
or you can use xmltodict
helpful library:
import xmltodict
with open('path/to/file.xml') as fd:
doc = xmltodict.parse(fd.read())
For more information look at This link.
add a comment |
There is a couple of libraries that you can use:
one is untangle
and can be installed by pip
like pip install untangle
:
import untangle
obj = untangle.parse('path/to/file.xml')
or you can use xmltodict
helpful library:
import xmltodict
with open('path/to/file.xml') as fd:
doc = xmltodict.parse(fd.read())
For more information look at This link.
There is a couple of libraries that you can use:
one is untangle
and can be installed by pip
like pip install untangle
:
import untangle
obj = untangle.parse('path/to/file.xml')
or you can use xmltodict
helpful library:
import xmltodict
with open('path/to/file.xml') as fd:
doc = xmltodict.parse(fd.read())
For more information look at This link.
answered Nov 13 '18 at 11:27
Mehrdad PedramfarMehrdad Pedramfar
4,89211237
4,89211237
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53279915%2fcreate-xml-with-multiple-root-elements-from-txt-file%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Have you considered using the built in
xml
library?– Kendas
Nov 13 '18 at 11:25
@Kendas: why consider that? It cannot handle such malformed XML data: "junk after document element: line 7, column 0".
– usr2564301
Nov 13 '18 at 12:26