shanti shanti - 2 months ago 12
Python Question

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 print
u
even though I used
str()
? I read in other post that using string will not print it in the std output.

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

Answer Source

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" ] }