6

I want to make google trasnlate script. I am making a request to translate.google.com and google return an array but the array contains undefined items.You can imagine response comes as string. I can remove commas if there is more than one consecutive with regex etc. but I am looking best solution :)

How can I convert this javascript array to python list?

["a","b",,,"e"]

My script : http://ideone.com/jhjZe

Mesut Tasci
  • 2,970
  • 1
  • 30
  • 36
  • 1
    @Marcin that won't work. this isn't valid [JSON](http://www.json.org/), so the parser will reject it. – mata May 17 '12 at 19:08
  • @mata Right, but if he has a real javascript array, he can just stop creating this string which isn't syntactically valid in either language. – Marcin May 17 '12 at 19:10
  • @Marcin: His `["a","b",,,"e"]` is equal to `["a", "b", undefined, undefined, "e"]` in JavaScript. See this: http://jsfiddle.net/Gtehr/. Maybe it differs across browsers, but this is what I get in Chrome. – Tadeck May 17 '12 at 19:13
  • @Tadeck So, I don't see the problem. He can still convert it to json as per your answer. – Marcin May 17 '12 at 19:15

1 Answers1

24

JavaScript part - encoding

In Javascript you do:

var arr = ["a","b",,,"e"];
var json_string = JSON.stringify(arr);

then you somehow pass json_string (now equal to "["a","b",null,null,"e"]" string) from JavaScript to Python.

Python part - decoding

Then, on Python side do:

json_string = '["a","b",null,null,"e"]'  # passed from JavaScript

try:
    import simplejson as json
except (ImportError,):
    import json

result = json.loads(json_string)

As a result you get [u'a', u'b', None, None, u'e'] in Python.

More links

See below:

Dependencies

The above solutions require:

  • JSON.stringify() in JavaScript, which is in all mobile browsers, Chrome, Firefox, Opera, Safari and in IE since version 8.0 (more detailed list of compatible browsers is here),
  • json Python library (the above code will use simplejson optionally, if available, but is not required), which comes in standard library,

So, in short there are no external dependencies.

Tadeck
  • 132,510
  • 28
  • 152
  • 198
  • I cant use JSON.stringify because JSON.stringify a javascript function .I cant use it .So there is no null.There is commas consecutively. – Mesut Tasci May 17 '12 at 19:16
  • @mesuutt: So you say you do not have access to JavaScript and you want to parse JavaScript variable from Python? – Tadeck May 17 '12 at 19:17
  • Yes I want to parse javascript array from python.You can look my script : [http://ideone.com/jhjZe](http://ideone.com/jhjZe) – Mesut Tasci May 17 '12 at 19:22
  • @mesuutt: Try [`python-spidermonkey`](http://code.google.com/p/python-spidermonkey/) then for example. The following may work for you: `from spidermonkey import Runtime; rt = Runtime(); cx = rt.new_context(); result = cx.eval_script('["a","b",,,"e"]')`. Does it work for you? – Tadeck May 17 '12 at 19:50
  • I am getting that spidermonkey.Context object has no attribute 'eval_script' error.There is another method instead of using library? – Mesut Tasci May 17 '12 at 20:10
  • What's the purpose of ImportError in a tuple? –  May 17 '12 at 20:25
  • What?.I do not understand you @pdknsk – Mesut Tasci May 17 '12 at 20:47
  • @pdknsk: This is redundant, but I consider it a good practice - when adding another exception to be caught by the same clause you will do it in a more readable way, avoiding issues described here: http://www.ibm.com/developerworks/linux/library/l-python3-2/#exceptions – Tadeck May 17 '12 at 21:12
  • @mesuutt: I was mostly quoting their tutorial (http://code.google.com/p/python-spidermonkey/). Yes, there are other methods, but using JavaScript parsing library (you are not limited to SpiderMonkey) is the most reliable method. Other methods would be just mostly walkarounds and hacks. – Tadeck May 17 '12 at 21:15
  • 1
    Ok.I thought solution of the problem is easy and I thought I can solve without using any additional library.I am solving with `re.sub("[,]{2,}","",str)` for now because script should run without any dependency(except basic libraries).Thanks – Mesut Tasci May 17 '12 at 21:44
  • @mesuutt: This is your choice, but if you need some reliability, try to find better regular expression for splitting. Consider eg. `["a,,b","b",,,"e"]`. Although this may work for you, if you are sure about the kind of values you receive. – Tadeck May 18 '12 at 02:02
  • 1
    I wrote code wrong.This work without any problem on any string : `re.sub("[,]{2,}",",",str)` :) – Mesut Tasci May 18 '12 at 07:17
  • @mesuutt: I have added more details on the fact that _there are no external dependencies_. If you disagree, please let me know. – Tadeck May 01 '14 at 15:05