Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #97214 > unrolled thread
| Started by | Peter Otten <__peter__@web.de> |
|---|---|
| First post | 2015-09-29 19:28 +0200 |
| Last post | 2015-09-29 19:28 +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.
Re: Create a .lua fle from Python Peter Otten <__peter__@web.de> - 2015-09-29 19:28 +0200
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Date | 2015-09-29 19:28 +0200 |
| Subject | Re: Create a .lua fle from Python |
| Message-ID | <mailman.244.1443547713.28679.python-list@python.org> |
jmp wrote:
> 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,
> }
Is Ariel's xml file user-supplied? If so, how does your suggestion prevent
the resulting lua script from executing arbitrary code?
Back to top | Article view | comp.lang.python
csiph-web