john pfeiffer
  • Home
  • Categories
  • Tags
  • Archives

unicode comparison json response prettyprint

# unicode comparison (json responses; json.dumps uses encoding="utf-8" by default
import requests
import json

path = u'/กกกก'.encode( 'utf-8' )

parameters = { 'path': path }
headers = Config.create_headers( Config.GET_METHOD, Config.DELETE_PATH, int( time.time() ), Config.TEST_APP_KEY, Config.TEST_APP_SECRET, queryParams=parameters )
response = requests.get( Config.HOST + Config.DELETE_PATH , headers=headers , params=parameters )


expected = unicode( path.decode( 'utf-8' ) + 'renamed' )

print type( unicode( 'renamed' ) ), type( data[4]['path'] )
print expected, data[4]['path']
assert expected == data[4]['path']


- - -
echo '{"foo": "abc", "bar": "xyz"}' | python -mjson.tool

cat deleteme.json | python -mjson.tool


print json.dumps({'a': 1, 'b': 2}, sort_keys=True, indent=4)

JSON.stringify({"foo":"lorem","bar":"ipsum"}, null, 4);     # 4 spaces indentation

JSON.stringify({"foo":"lorem","bar":"ipsum"}, null, '\t');  # tab indentation


- - -

# -*- coding: utf-8 -*-
import codecs
import sys

UTF8Writer = codecs.getwriter('utf8')
sys.stdout = UTF8Writer(sys.stdout)
print u'café + сайта'

  • « Vi vim hotkeys navigation editing whitespace line numbers
  • Nmap »

Published

Apr 8, 2015

Category

python

~93 words

Tags

  • comparison 4
  • json 3
  • prettyprint 1
  • python 180
  • response 1
  • unicode 2