On 09/28/2015 11:41 PM, Ariel Argañaraz wrote:
Hi,
This is my first post, I would like to know if a library that can help
me with this.


I want to parse a XML fle with Python and save the data into a Lua table
called for example "newTable", then I want to create a "table.lua" fle
with the "newTable" write on it.

And  I want to create a cities_temp.lua file

cities_temps ={
["Buenos Aires"] = 30,
["Seatle"] = 25,
}
Can anyone give some help?

Thanks.

--
Ariel Argañaraz

Use an xml parser to fetch the data from the xml file and use a template engine to generate the lua code.

for instance, using bs4 (beautifulsoup) for xml and jinja2 for the template engine:

import bs4
import jinja2

xml = """<cities>
     <city>
              <name>BuenosAires</name>
              <temperature>30</temperature>
      </city>
<city>
      <name>Seatle</name>
      <temperature>25</temperature>
</city>
</cities>"""

lua_template = """
cities_temps ={
{%- for city, temp in cities.iteritems() %}
["{{city}}"] = {{temp}},
{%- endfor %}
}"""

xmlp = bs4.BeautifulSoup(xml, 'xml')
# from xml to python dictionary
data = {city.find('name').string:city.find('temperature').string for city in xmlp.findAll('city')}
# from python dictionary to lua
print jinja2.Template(lua_template).render(cities=data)


will yield (python 2.7):

cities_temps ={
["BuenosAires"] = 30,
["Seatle"] = 25,
}

Cheers,

jm



--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to