Running Python Oneliners From Powershell Terminal Exemplified On JSON Minifier
1 min readSep 3, 2021
The aim of this tutorial🔍 is to provide the simple rules for running python code python -c <code>
from PowerShell. The basic idea is the code has to be formatted with PowerShell special characters according to the Python syntax requirements. I’ve used this to quickly convert a json file into a minified version.
I am using this for quick checks of commands when I don’t want to switch between PowerShell and python repl, for example just checking for the existence of the file.
â–¶ python -c "import os`nprint(os.path.exists('./helpers/temp_newDoc_indexItem.yaml'))"
True
# cool, file existence verified
1. instructions
- enclose the whole code section in
"
- so that you evaluate special characters - use
'
for quotes within the code section - use `n as line separators
- use `t as tab character essential for python indentation
2. example: JSON minifier
- … append
|clip
to copy output into clipboard :)
python -c "import json`nimport sys`nwith open(sys.argv[1], 'r') as f:`n`tprint(json.dumps(json.load(f), separators=(',',':')))" file.json
3. on separators keyword
- If specified, separators should be an
(item_separator, key_separator)
tuple. - The default is
(', ', ': ')
if indent isNone
and(',', ': ')
otherwise. - To get the most compact JSON representation, you should specify
(',', ':')
to eliminate whitespace.
— https://docs.python.org/3/library/json.html