Python js-beautifier fails when script contains UTF-8, and output is piped
See original GitHub issue$ cat test.js
var s = "åäö";
$ js-beautify test.js
var s = "åäö";
$ js-beautify test.js | cat -
'ascii' codec can't encode characters in position 9-11: ordinal not in range(128)
$ js-beautify --version
1.6.4
$ python --version
Python 2.7.12
Issue Analytics
- State:
- Created 7 years ago
- Reactions:1
- Comments:11 (10 by maintainers)
Top Results From Across the Web
Error using jsbeautifier in python with unicode text
I use the following code to beautify a js file (with jsbeautifier module) using python (3.4)
Read more >Why do I get a Unicode error when piping output containing ...
If I update my script to Python 3 and run it like python3 -c 'print ("diameter "+ unichr(0x2300))'|grep 'd' it works. This means...
Read more >js-beautify | Yarn - Package Manager
Node.js JavaScript. When installed globally, the beautifier provides an executable js-beautify script. The beautified result is sent to stdout unless otherwise ...
Read more >2007-March.txt - Python mailing list
[TO] results in a list containing a single string - not a list containing 3 ... This failure > occurs for all redirection,...
Read more >Untitled
BSF supports several scripting languages currently: * Javascript (using Rhino ECMAScript, from the Mozilla project) * Python (using either Jython or ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hey @bitwiseman, did this get fixed by chance? I looked through recent changes here https://github.com/beautify-web/js-beautify/commits/master but didn’t find any relevant ones.
I’m on: EditorConfig (0.12.1) jsbeautifier (1.6.12)
I tested it on Python 2.7.9 thru 2.7.13 on both brew and pkg’s to try to get it to work, only to find that the bug seems to manifest always when redirecting the output, regardless of version.
It works as intended if I do not redirect the output (and the program outputs to stdout), but as soon as I pipe or redirect the output to anything, the program shatters (the output does not get correctly redirected) and emits to stderr a message such as:
‘ascii’ codec can’t encode character u’\u…’ in position 376942: ordinal not in range(128)
This makes using js-beautify an excellent adventure in copy/paste when beautifying larger files.
And just now reading @tobbez’s above proposed change which I somehow failed to read before debugging it, that solution should fix it exactly.
Bump 😃
Putting it right at the top of the file is fine as long as it’s an application (and was actually intended for that).
Anyway, I didn’t look too closely at the existing code before writing the above, but doing so makes it clear that the above code is much more general than is needed here. That solution is useful when writing to standard out in many places, but seeing as js-beautifier only writes output in a single place, the following should suffice: