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


Groups > comp.lang.python > #97212

Re: Create a .lua fle from Python

Path csiph.com!news.swapon.de!eternal-september.org!feeder.eternal-september.org!border1.nntp.ams1.giganews.com!nntp.giganews.com!newsfeed.xs4all.nl!newsfeed7.news.xs4all.nl!nzpost1.xs4all.net!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'subject:Python': 0.05; '"""': 0.05; '(python': 0.05; 'fetch': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'python': 0.10; 'template': 0.11; 'help?': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'temp': 0.16; 'wrote:': 0.16; 'instance,': 0.18; 'thanks.': 0.18; 'library': 0.20; 'parse': 0.22; 'parser': 0.22; 'subject: .': 0.22; 'cheers,': 0.22; 'code.': 0.23; 'import': 0.24; 'xml': 0.24; 'header:In-Reply-To:1': 0.24; 'header:User-Agent:1': 0.26; 'example': 0.26; 'header:X-Complaints-To:1': 0.26; 'yield': 0.27; 'this.': 0.28; 'dictionary': 0.29; 'print': 0.30; 'skip:[ 10': 0.31; 'anyone': 0.32; 'table': 0.32; 'file': 0.34; 'to:addr :python-list': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'received:org': 0.37; 'hi,': 0.38; 'data': 0.39; 'subject:from': 0.39; 'to:addr:python.org': 0.40; 'called': 0.40; 'some': 0.40; 'save': 0.60; 'received:194': 0.61; 'engine': 0.62; 'charset:windows-1252': 0.62; '30,': 0.63; 'city': 0.65; 'post,': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From jmp <jeanmichel@sequans.com>
Subject Re: Create a .lua fle from Python
Date Tue, 29 Sep 2015 18:32:15 +0200
References <CAMxmM6bhyHUpQXxnwO-OiesppP6mRM74=dA37VJwV212MZGyUw@mail.gmail.com>
Mime-Version 1.0
Content-Type text/plain; charset=windows-1252; format=flowed
Content-Transfer-Encoding 8bit
X-Gmane-NNTP-Posting-Host paris.sequans.com
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.6.0
In-Reply-To <CAMxmM6bhyHUpQXxnwO-OiesppP6mRM74=dA37VJwV212MZGyUw@mail.gmail.com>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.243.1443544349.28679.python-list@python.org> (permalink)
Lines 71
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1443544349 news.xs4all.nl 23720 [2001:888:2000:d::a6]:33508
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:97212

Show key headers only | View raw


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


Thread

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

csiph-web