Error using jsbeautifier in python with unicode text
Question: I use the following code to beautify a js file (with jsbeautifier module)
using python (3.4)

    
    
    import jsbeautifier
    
    def write_file(output, fn):
        file = open(fn, "w")
        file.write(output)
        file.close()
    
    def beautify_file():
        res = jsbeautifier.beautify_file("myfile.js")
        write_file(res, "myfile-exp.js")
        print("beautify_file done")
    
    def main():
        beautify_file()
        print("done")
        pass
    
    if __name__ == '__main__':
        main()
    

The file contains the following contents:

    
    
    function MyFunc(){
      return {Language:"ÐœÐ¾Ð²Ð°",Theme:"Ð¢ÑÐ¼Ð°"};
    }
    

When I run the python code, I get the following error:

    
    
    'charmap' codec can't decode byte 0x90 in position 43: character maps to <undefined>
    

Can someone guide me as to how to handle unicode/utf-8 charsets with the
beautifier?

Thanks


Answer: It's hard to tell without a full stack trace but it looks like jsbeautify
isn't fully Unicode aware.

Try one of the following:

  1. Decode js file to Unicode:
    
        with open("myfile.js", "r", encoding="UTF-8") as myfile:
        input_string = myfile.read()
        res = jsbeautifier.beautify(input_string)
    

or, if that fails

  2. Open file as binary:
    
        with open("myfile.js", "rb") as myfile:
        input_string = myfile.read()
        res = jsbeautifier.beautify(input_string)
    

In addition, you may run into issues when writing. You really need to set the
encoding on the output file:

    
    
    file = open(fn, "w", encoding="utf-8")
    

