How to convert JavaScript dictionary into Python syntax

Posted by Sputnix on Stack Overflow See other posts from Stack Overflow or by Sputnix
Published on 2014-05-31T03:23:42Z Indexed on 2014/05/31 3:24 UTC
Read the original article Hit count: 247

Filed under:
|

Writing out javascript dictionary from inside of JavaScript- enabled application (such as Adobe) into external .jsx file (or any other .txt file) the context of resulted file dictionary looks like:

({one:"1", two:"2"})

(Please note that each dictionary keys are written as they are the variables name (which is not true). A next step is to read this .jsx file with Python. I need to find a way to convert ({one:"1", two:"2"}) into Python dictionary syntax such as:

{'one':"1", 'two':"2"}

It has been already suggested that instead of using JavaScript's built-in dict.toSource() it would make more sense to use JSON which would write a dictionary content in similar to Python syntax. But unfortunately using JSON is not an option for me. I need to find a way to convert ({one:"1", two:"2"}) into {'one':"1", 'two':"2"} using Python alone. Any suggestions on how to achieve it? Once again, the problem mostly in dictionary keys syntax which inside of Python look like variable names instead of strings-like dictionary keys names:

one vs "one"

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about python