Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #97212
| From | jmp <jeanmichel@sequans.com> |
|---|---|
| Subject | Re: Create a .lua fle from Python |
| Date | 2015-09-29 18:32 +0200 |
| References | <CAMxmM6bhyHUpQXxnwO-OiesppP6mRM74=dA37VJwV212MZGyUw@mail.gmail.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.243.1443544349.28679.python-list@python.org> (permalink) |
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
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: Create a .lua fle from Python jmp <jeanmichel@sequans.com> - 2015-09-29 18:32 +0200
csiph-web