Search code examples
pythonpython-2.7unicodejqpython-unicode

Why are 'u' prefixes still printing even though I used str()?


New to Python, Python version: 2.7.10, Machine: Mac OS Sierra.

Susi Sushanti Don $ python -c "import sys, json; print(json.load(open('/tmp/2.json'))['pages'])"
{u'giga-10': [u'overview']}

Susi Sushanti Don $ python -c "import sys, json; print(str(json.load(open('/tmp/2.json'))['pages']))"
{u'giga-10': [u'overview']}

Why is python still printing u character even though I used str()? I read in other post that using string will not print it in the std output. Is there any similar str() function which can work on any Python data object (rather than writing a reusable function myself)?

I'm expecting the output to be just {'giga-10': ['overview']}


Solution

  • For this, why not use jq JQ utility for a one liner.

    You can achieve the:

    $ echo `jq ".pages" /tmp/2.json`
    { "giga-10": [ "overview" ] }
    

    Don't forget to check out this URL: https://jqplay.org/ it really helped me learn / watch how jq will play with the input data.