Lassen Sie uns sagen, dass ich diesen Code in einer JavaScript-Datei:Wie kann ich JavaScript-Code von Python ausführen?
var x = 10;
x = 10 - 5;
console.log(x);
function greet() {
console.log("Hello World!");
}
greet()
Wie würde ich Python verwenden diesen Code und "print"x
und Hello World!
auszuführen?
Hier einige Pseudo-Code, der weiter erklärt, was ich denke:
# 1. open the script
script = open("/path/to/js/files.js", "r")
# 2. get the script content
script_content = script.read()
# 3. close the script file
script.close()
# 4. execute the script content and "print" "x" and "Hello World!"
x = js.exec(script_content)
Und würde das erwartete Ergebnis wie folgt aussehen:
>>> 5
>>> "Hello World!"
Dies kann helfen: https://pypi.python.org/pypi/PyExecJS – FailedUnitTest
@FailedUnitTest Vielen Dank für den Link. Kann der Down-Voter auch erklären, warum & wie dies zu weit gefasst ist? –