Text file to XML representation

K

kaklis

Hello,
I would like to make a program that takes a text file with the
following representation:

outlook = sunny
| humidity <= 70: yes (2.0)
| humidity > 70: no (3.0)
outlook = overcast: yes (4.0)
outlook = rainy
| windy = TRUE: no (2.0)
| windy = FALSE: yes (3.0)

and convert it to xml file for example:
<?xml version="1.0" encoding="UTF-8"?>
<DecisionTree type="DescisionTree">
<Test attribute="outlook" operator="=" value="sunny">
<Test attribute="humidity" operator="&lt;=" value="70">
<Output decision="yes" info="(2.0)"/>
</Test>
<Test attribute="humidity" operator=">" value="70">
<Output decision="no" info="(3.0)"/>
</Test>
</Test>
<Test attribute="outlook" operator="=" value="overcast">
<Output decision="yes" info="(4.0)"/>
</Test>
<Test attribute="outlook" operator="=" value="rainy">
<Test attribute="windy" operator="=" value="TRUE">
<Output decision="no" info="(2.0)"/>
</Test>
<Test attribute="windy" operator="=" value="FALSE">
<Output decision="yes" info="(3.0)"/>
</Test>
</Test>
</DecisionTree>

Is there a way to do it?
Please help
Thanks in advance
Antonis
 
B

Bruno Desthuilliers

(e-mail address removed) a écrit :
Hello,
I would like to make a program that takes a text file with the
following representation:

outlook = sunny
| humidity <= 70: yes (2.0)
| humidity > 70: no (3.0)
outlook = overcast: yes (4.0)
outlook = rainy
| windy = TRUE: no (2.0)
| windy = FALSE: yes (3.0)

and convert it to xml file for example:

(snip xml)
Is there a way to do it?

More than one. But I'd stronly suggest something like PyParsing +
ElementTree.

PyParsing : http://pyparsing.wikispaces.com/

ElementTree : is now in the stdlib, so refer to the FineManual
 
J

Jorgen Grahn

Hello,
I would like to make a program that takes a text file with the
following representation:

outlook = sunny
| humidity <= 70: yes (2.0)
| humidity > 70: no (3.0)
outlook = overcast: yes (4.0)
outlook = rainy
| windy = TRUE: no (2.0)
| windy = FALSE: yes (3.0)

and convert it to xml file for example:
<?xml version="1.0" encoding="UTF-8"?>
<DecisionTree type="DescisionTree"> ....
</DecisionTree>

Is there a way to do it?

No. Impossible.

No, of course it is possible. I'd think of it as a problem of (a)
making clear to yourself what the input format (language) is, (b)
write a parser for it (which transform it to Python data structures
and (c) write code to dump the data structures according to some DTD
(or whatever the XML people call it these days).

(c) seems to be the easy part.

/Jorgen
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top