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


Groups > comp.lang.python > #12655

Re: convert python List to javascript array

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!feeder.news-service.com!news2.euro.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <chris@rebertia.com>
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; 'javascript,': 0.05; 'e.g.,': 0.07; 'json': 0.07; 'python': 0.08; 'format?': 0.09; 'framework,': 0.09; 'parsing': 0.09; 'programmers.': 0.09; 'api': 0.09; 'subject:python': 0.11; 'am,': 0.12; 'case.': 0.15; '(whether': 0.16; 'likewise': 0.16; 'subject:array': 0.16; 'subset': 0.16; 'tuple)': 0.16; 'cc:addr:python-list': 0.16; 'wrote:': 0.16; 'cheers,': 0.18; '>>>': 0.18; 'convert': 0.19; 'cc:2**0': 0.22; 'header:In-Reply-To:1': 0.22; 'sep': 0.23; 'subject:List': 0.25; 'function': 0.27; 'fact': 0.27; 'problem': 0.28; 'correct': 0.28; 'import': 0.28; 'pass': 0.29; 'message- id:@mail.gmail.com': 0.29; 'cc:addr:python.org': 0.30; 'array': 0.30; 'module': 0.30; 'url:developer': 0.30; 'list:': 0.31; 'url:library': 0.31; 'chris': 0.32; 'values': 0.32; 'list': 0.32; 'url:python': 0.36; 'fri,': 0.36; 'sequence': 0.37; 'but': 0.37; 'received:google.com': 0.38; 'url:org': 0.38; 'received:209.85': 0.38; 'subject:: ': 0.39; 'url:docs': 0.39; 'url:en': 0.39; 'basic': 0.39; 'browsers': 0.67; 'to:addr:yahoo.com': 0.83; 'client-side': 0.84; 'compatible)': 0.84; 'sender:addr:chris': 0.84; 'url:mozilla': 0.84; 'url:rebertia': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=rebertia.com; s=google; h=mime-version:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:cc:content-type :content-transfer-encoding; bh=6StT/4mVwbUjhprZfRLMpH7Ga7Ln34lzFNrwGgTQZFw=; b=NpdKCvRI0Fj5D9xnHsQerDs5oAczaunqe8wKGs8oPs/q8uu+OLIL5mFEyMOiOgrfxS 6Z9+v+MxGgLi7oaILYx62snRMGXALkAZeyhKfPNv43+Pm0bt+SWtBLwkrVQ9s8Stkqnu sFpoUbyzDAiH1zGgpgoJqluf+joyiDPBjkyAc=
MIME-Version 1.0
Sender chris@rebertia.com
In-Reply-To <1314977665.30984.YahooMailNeo@web160510.mail.bf1.yahoo.com>
References <1314977665.30984.YahooMailNeo@web160510.mail.bf1.yahoo.com>
Date Fri, 2 Sep 2011 10:26:15 -0700
X-Google-Sender-Auth rvrLzPHm7GXDssYH4OVhm_iuojA
Subject Re: convert python List to javascript array
From Chris Rebert <clp2@rebertia.com>
To Vineet Deodhar <d_vineet@yahoo.com>
Content-Type text/plain; charset=UTF-8
Content-Transfer-Encoding quoted-printable
Cc "python-list@python.org" <python-list@python.org>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://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 <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.713.1314984377.27778.python-list@python.org> (permalink)
Lines 33
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1314984377 news.xs4all.nl 2441 [2001:888:2000:d::a6]:59587
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:12655

Show key headers only | View raw


On Fri, Sep 2, 2011 at 8:34 AM, Vineet Deodhar <d_vineet@yahoo.com> wrote:
> Hi !
> Within a web framework, I want want to pass a python sequence (list or
> tuple) to client-side javascript function as an array (javascript
> compatible)
> e.g., I have this list:
> L = ['spam', 'ham', 'eggs', 12, (13.63)]
> What is the correct way to convert L to javascript array format?
> 1) jsonify the list and pass it to javascript
> (whether json format & javascript array are similar?)

JSON is in fact a subset of JavaScript, and modern browsers now
include a specific API for parsing and generating it
(https://developer.mozilla.org/En/Using_native_JSON ).
Python likewise has a JSON module in the std lib:
http://docs.python.org/library/json.html

> OR
> 2)
>>> import array
>>> y = array.array(i, L)
>  then return y to javascript function
> But the problem with this method is, it will give an array of basic values
> only.

The word "array" gets tossed around a lot by programmers. The `array`
module is not at all what you want in this case.

Cheers,
Chris
--
http://rebertia.com

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: convert python List to javascript array Chris Rebert <clp2@rebertia.com> - 2011-09-02 10:26 -0700

csiph-web