7

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

4
  • 1
    @Marcin that won't work. this isn't valid JSON, so the parser will reject it. Commented May 17, 2012 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. Commented May 17, 2012 at 19:10
  • @Marcin: His ["a","b",,,"e"] is equal to ["a", "b", undefined, undefined, "e"] in JavaScript. See this: jsfiddle.net/Gtehr. Maybe it differs across browsers, but this is what I get in Chrome. Commented May 17, 2012 at 19:13
  • @Tadeck So, I don't see the problem. He can still convert it to json as per your answer. Commented May 17, 2012 at 19:15

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

14 Comments

I cant use JSON.stringify because JSON.stringify a javascript function .I cant use it .So there is no null.There is commas consecutively.
@mesuutt: So you say you do not have access to JavaScript and you want to parse JavaScript variable from Python?
Yes I want to parse javascript array from python.You can look my script : http://ideone.com/jhjZe
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
I wrote code wrong.This work without any problem on any string : re.sub("[,]{2,}",",",str) :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.