Python header unicode to dict
Question: I have a MySQL database with a python request header saved in one of the
columns and it looks something like this:

    
    
    {
     '_': '/Users/user/.virtualenvs/squadraft/bin/python',
     'wsgi.multiprocess': False, 'RUN_MAIN': 'true',
     'rvm_version': '1.26.11 (latest)',
     'HTTP_ACCEPT_LANGUAGE': 'en-US,en;q=0.8,fr;q=0.6',
     'PIP_RESPECT_VIRTUALENV': 'true',
     'SERVER_SOFTWARE': 'WSGIServer/0.1 Python/2.7.5',
     'rvm_path': '/Users/user/.rvm',
     'TERM_PROGRAM_VERSION': '326',
     'RUBY_VERSION': 'ruby-2.0.0-p643',
     'REQUEST_METHOD': 'POST',
     'LOGNAME': 'user',
     'USER': 'user',
     'HTTP_ORIGIN': 'chrome-extension://fhbjgbiflinjbdggehcddcbncdddomop',
     'PATH': '/Users/user/.virtualenvs/squadraft/bin:/Users/user/.rvm/gems/ruby-2.0.0-p643/bin:/Users/user/.rvm/gems/ruby-2.0.0-p643@global/bin:/Users/user/.rvm/rubies/ruby-2.0.0-p643/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/local/sbin:/Users/user/.rvm/bin:/Users/user/.rvm/bin',
     'QUERY_STRING': '',
     'HOME': '/Users/user',
     'VIRTUALENVWRAPPER_SCRIPT': '/usr/local/bin/virtualenvwrapper.sh',
     'HTTP_CONNECTION': 'keep-alive',
     'TERM_PROGRAM': 'Apple_Terminal',
     'LANG': 'en_US.UTF-8',
     'TERM': 'xterm-256color',
     'SHELL': '/bin/bash',
     'TZ': 'UTC',
     'SERVER_NAME': '1.0.0.127.in-addr.arpa',
     'VERSIONER_PYTHON_VERSION': '2.7',
     'SHLVL': '1',
     '_system_name': 'OSX',
     'wsgi.url_scheme': 'http',
     'VIRTUALENVWRAPPER_VIRTUALENV_ARGS': '--no-site-packages',
     'SERVER_PORT': '8000',
     'CONTENT_LENGTH': '38414',
     'SERVER_PROTOCOL': 'HTTP/1.1',
     '_system_arch': 'x86_64',
     'GEM_PATH': '/Users/user/.rvm/gems/ruby-2.0.0-p643:/Users/user/.rvm/gems/ruby-2.0.0-p643@global',
     'rvm_bin_path': '/Users/user/.rvm/bin',
     'WORKON_HOME': '/Users/user/.virtualenvs',
     'TERM_SESSION_ID': '8CFC3FA5-6F56-49B7-AFAB-A807AFFA1D83',
     'VERSIONER_PYTHON_PREFER_32_BIT': 'no',
     'CONTENT_TYPE': 'application/xml',
     'rvm_prefix': '/Users/user',
     'HTTP_POSTMAN_TOKEN': 'c084799e-820d-7f70-4b7e-7597f1abab6f',
     'Apple_PubSub_Socket_Render': '/tmp/launch-g9J1iQ/Render',
     'SSH_AUTH_SOCK': '/tmp/launch-JlZPBH/Listeners',
     'VIRTUAL_ENV': '/Users/user/.virtualenvs/squadraft',
     'LC_CTYPE': 'UTF-8',
     'IRBRC': '/Users/user/.rvm/rubies/ruby-2.0.0-p643/.irbrc',
     'MY_RUBY_HOME': '/Users/user/.rvm/rubies/ruby-2.0.0-p643',
     'HTTP_CACHE_CONTROL': 'no-cache',
     'VIRTUALENVWRAPPER_WORKON_CD': '1',
     'PS1': '(squadraft) \h:\W \u\$ ',
     'SCRIPT_NAME': u'',
     'wsgi.multithread': True, 'LC_ALL': 'en_US.UTF-8',
     '_system_type': 'Darwin',
     'TMPDIR': '/var/folders/5c/vhcdkw8n6xz2n_ywlkvt2q_r0000gn/T/',
     'HTTP_ACCEPT': '*/*',
     'VIRTUALENVWRAPPER_PROJECT_FILENAME': '.project',
     'wsgi.version': (1, 0), 'VIRTUALENVWRAPPER_HOOK_DIR': '/Users/user/.virtualenvs',
     'GATEWAY_INTERFACE': 'CGI/1.1',
     'wsgi.run_once': False, 'REMOTE_HOST': '',
     'OLDPWD': '/Users/user/Development/rotas',
     'REMOTE_ADDR': '127.0.0.1',
     'HTTP_USER_AGENT': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 Safari/537.36',
     'wsgi.input': <socket._fileobject object at 0x112519cd0>, 
     'wsgi.errors': <open file '<stderr>', mode 'w' at 0x10f4891e0>, 
     'HTTP_ACCEPT_ENCODING': 'gzip, deflate',
     '__CF_USER_TEXT_ENCODING': '0x1F5:0:0',
     'PWD': '/Users/user/Development/squadraft',
     'PIP_VIRTUALENV_BASE': '/Users/user/.virtualenvs',
     'DJANGO_SETTINGS_MODULE': 'fantasysport.settings.local',
     '_system_version': '10.9',
     'HTTP_HOST': '127.0.0.1:8000',
     'wsgi.file_wrapper': <class wsgiref.util.FileWrapper at 0x110994258>,
     '__CHECKFIX1436934': '1',
     'GEM_HOME': '/Users/user/.rvm/gems/ruby-2.0.0-p643',
     'PATH_INFO': u'/client/collect-data/'
    }
    

Note accessing it through my model query I get it as Unicode. I would like to
convert this into a dictionary so that I can access the underlying properties.

Here are some of the things I have tried so far.

    
    
    import json
    json.loads(header_data)
    # This fails with a 'ValueError: Expecting property name 
    # enclosed in double quotes: line 1 column 2 (char 1)'
    
    # Also
    import ast
    ast.literal_eval(header_data)
    # The above fails with a 'File "<unknown>", line 1' error
    

More information:

    
    
    print(type(header_data))
    <type 'unicode'>
    
    print(header_data)
    {'_': '/Users/user/.virtualenvs/squadraft/bin/python', 'wsgi.multiprocess': False, 'RUN_MAIN': 'true', 'rvm_version': '1.26.11 (latest)', 'HTTP_ACCEPT_LANGUAGE': 'en-US,en;q=0.8,fr;q=0.6', 'PIP_RESPECT_VIRTUALENV': 'true', 'SERVER_SOFTWARE': 'WSGIServer/0.1 Python/2.7.5', 'rvm_path': '/Users/user/.rvm', 'TERM_PROGRAM_VERSION': '326', 'RUBY_VERSION': 'ruby-2.0.0-p643', 'REQUEST_METHOD': 'POST', 'LOGNAME': 'user', 'USER': 'user', 'HTTP_ORIGIN': 'chrome-extension://fhbjgbiflinjbdggehcddcbncdddomop', 'PATH': '/Users/user/.virtualenvs/squadraft/bin:/Users/user/.rvm/gems/ruby-2.0.0-p643/bin:/Users/user/.rvm/gems/ruby-2.0.0-p643@global/bin:/Users/user/.rvm/rubies/ruby-2.0.0-p643/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/local/sbin:/Users/user/.rvm/bin:/Users/user/.rvm/bin', 'QUERY_STRING': '', 'HOME': '/Users/user', 'VIRTUALENVWRAPPER_SCRIPT': '/usr/local/bin/virtualenvwrapper.sh', 'HTTP_CONNECTION': 'keep-alive', 'TERM_PROGRAM': 'Apple_Terminal', 'LANG': 'en_US.UTF-8', 'TERM': 'xterm-256color', 'SHELL': '/bin/bash', 'TZ': 'UTC', 'SERVER_NAME': '1.0.0.127.in-addr.arpa', 'VERSIONER_PYTHON_VERSION': '2.7', 'SHLVL': '1', '_system_name': 'OSX', 'wsgi.url_scheme': 'http', 'VIRTUALENVWRAPPER_VIRTUALENV_ARGS': '--no-site-packages', 'SERVER_PORT': '8000', 'CONTENT_LENGTH': '38414', 'SERVER_PROTOCOL': 'HTTP/1.1', '_system_arch': 'x86_64', 'GEM_PATH': '/Users/user/.rvm/gems/ruby-2.0.0-p643:/Users/user/.rvm/gems/ruby-2.0.0-p643@global', 'rvm_bin_path': '/Users/user/.rvm/bin', 'WORKON_HOME': '/Users/user/.virtualenvs', 'TERM_SESSION_ID': '8CFC3FA5-6F56-49B7-AFAB-A807AFFA1D83', 'VERSIONER_PYTHON_PREFER_32_BIT': 'no', 'CONTENT_TYPE': 'application/xml', 'rvm_prefix': '/Users/user', 'HTTP_POSTMAN_TOKEN': 'c084799e-820d-7f70-4b7e-7597f1abab6f', 'Apple_PubSub_Socket_Render': '/tmp/launch-g9J1iQ/Render', 'SSH_AUTH_SOCK': '/tmp/launch-JlZPBH/Listeners', 'VIRTUAL_ENV': '/Users/user/.virtualenvs/squadraft', 'LC_CTYPE': 'UTF-8', 'IRBRC': '/Users/user/.rvm/rubies/ruby-2.0.0-p643/.irbrc', 'MY_RUBY_HOME': '/Users/user/.rvm/rubies/ruby-2.0.0-p643', 'HTTP_CACHE_CONTROL': 'no-cache', 'VIRTUALENVWRAPPER_WORKON_CD': '1', 'PS1': '(squadraft) \h:\W \u\$ ', 'SCRIPT_NAME': u'', 'wsgi.multithread': True, 'LC_ALL': 'en_US.UTF-8', '_system_type': 'Darwin', 'TMPDIR': '/var/folders/5c/vhcdkw8n6xz2n_ywlkvt2q_r0000gn/T/', 'HTTP_ACCEPT': '*/*', 'VIRTUALENVWRAPPER_PROJECT_FILENAME': '.project', 'wsgi.version': (1, 0), 'VIRTUALENVWRAPPER_HOOK_DIR': '/Users/user/.virtualenvs', 'GATEWAY_INTERFACE': 'CGI/1.1', 'wsgi.run_once': False, 'REMOTE_HOST': '', 'OLDPWD': '/Users/user/Development/rotas', 'REMOTE_ADDR': '127.0.0.1', 'HTTP_USER_AGENT': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 Safari/537.36', 'wsgi.input': <socket._fileobject object at 0x112519cd0>, 'wsgi.errors': <open file '<stderr>', mode 'w' at 0x10f4891e0>, 'HTTP_ACCEPT_ENCODING': 'gzip, deflate', '__CF_USER_TEXT_ENCODING': '0x1F5:0:0', 'PWD': '/Users/user/Development/squadraft', 'PIP_VIRTUALENV_BASE': '/Users/user/.virtualenvs', 'DJANGO_SETTINGS_MODULE': 'fantasysport.settings.local', '_system_version': '10.9', 'HTTP_HOST': '127.0.0.1:8000', 'wsgi.file_wrapper': <class wsgiref.util.FileWrapper at 0x110994258>, '__CHECKFIX1436934': '1', 'GEM_HOME': '/Users/user/.rvm/gems/ruby-2.0.0-p643', 'PATH_INFO': u'/opta/collect-data/'}
    


Answer: As had already been pointed out in a deleted answer, your problem are the
entries

    
    
    'wsgi.input': <socket._fileobject object at 0x112519cd0>
    'wsgi.errors': <open file '<stderr>', mode 'w' at 0x10f4891e0>
    'wsgi.file_wrapper': <class wsgiref.util.FileWrapper at 0x110994258>
    

Remove these and

    
    
    ast.literal_eval(header_data)
    

should work.

