Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #97212 > unrolled thread

Re: Create a .lua fle from Python

Started byjmp <jeanmichel@sequans.com>
First post2015-09-29 18:32 +0200
Last post2015-09-29 18:32 +0200
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Create a .lua fle from Python jmp <jeanmichel@sequans.com> - 2015-09-29 18:32 +0200

#97212 — Re: Create a .lua fle from Python

Fromjmp <jeanmichel@sequans.com>
Date2015-09-29 18:32 +0200
SubjectRe: Create a .lua fle from Python
Message-ID<mailman.243.1443544349.28679.python-list@python.org>
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


[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web