python3 installation of ExecJS

Refer to the official documentation installed

pip3 install PyExecJS 

Coding

execjs Import 
CTX = execjs.compile ( "" "
        function the Add (X, Y) {
                return X + Y; 
          } 
" "" ) after acquiring the code compiled objects # 
Print (ctx.call ( " the Add " , . 1 , 2 )) # . 3  
# Print (ctx.eval ( " the Add ({0}, {}. 1) " ) .format ( . 1 , 2 )) # error 
Print (ctx.eval ( ' the Add ( "{0}", " . 1} { ") ' ) .format ( " . 1 " , " 2 ")) # 12

File read write code

Create a file jsCode.js

function add(x, y) {
    return x + y;
}

Code execution

import execjs
file = 'jsCode.js'
ctx = execjs.compile(open(file).read())
js = 'add("{0}", "{1}")'.format("1","2")
params = ctx.eval(js)
print(params) # 12
params = ctx.call('add',1,2)
print(params) # 3

An error message appears

UnicodeEncodeError: 'gbk' codec can't encode character xxx

A Solution

Add toencoding="utf-8"

 
ctx = execjs.compile(open(file,encoding="utf-8").read())

Solution two

js file GBk way to save

 

Guess you like

Origin www.cnblogs.com/xiaoqianbook/p/11243689.html