Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

196
Vistas
¿Cómo devolver un objeto json desde una función javascript que ejecuta un script de python?

Estoy tratando de devolver un objeto json desde una función de javascript que ejecuta un script de python.

Tengo un script de python cuyo objetivo es crear una lista como la siguiente (aquí solo proporciono el resultado).

 mydict ={ "counterParty_Car": "Ford", "counterParty_CarPlate" : "O XXX YYY", "counterParty_Name" : "John Snow", "counterParty_Policy": "0123456789" } print(mydict)

este script de python se inicia mediante una función en javascript usando pyhton-shell

 function runPythonScript(pythonPath, pythonScript){ console.log('ND - server - run python without argument'); let myPath = __dirname; console.log('myPath: ', myPath); //let pythonPath = pythonPath; console.log('pythonPath: ', pythonPath); //let pythonScript = pythonScript; console.log('pythonScript: ', pythonScript); //Here are the option object in which arguments can be passed for the python_test.js. let options = { mode: 'text', pythonOptions: ['-u'], // get print results in real-time scriptPath: myPath + pythonPath //If you are having python_test.py script in same folder, then it's optional. //args: ['xxxx'] //An argument which can be accessed in the script using sys.argv[1] }; PythonShell.run(pythonScript, options, function (err, result){ if (err) throw err; // result is an array consisting of messages collected //during execution of script. console.log('data: ', result.toString()); let parsedData = JSON.parse(JSON.stringify(result.join('\n'))); console.log('data type: ',typeof parsedData, ' of length ', parsedData.length); parsedData = parsedData.replace(/'/g, `"`); // replace the quote by the double quotes for parsing the string to a JSON object. parsedData = JSON.parse(parsedData); console.log('parsed data type: ', typeof parsedData); console.log('parsed data: ', parsedData); return parsedData; }); }

En javascript finalmente llamo a runPythonScript como se muestra a continuación

 let counterpartyData = runPythonScript('/python','test.py'); console.log('ND - server - counterparty data: ', counterpartyData);

Pero cuando verifico el resultado en la consola, veo que los datos de la counterpartyData no están definidos. ¿Tienes una idea de por qué?

 ND - server - run python without argument myPath: /app pythonPath: /python pythonScript: test.py [...] parsed data: { counterParty_Car: 'Ford', counterParty_CarPlate: 'O XXX YYY', counterParty_Name: 'John Snow', counterParty_Policy: '0123456789' } ND - server - counterparty data: undefined
about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda