-1

I want to replace the character "u '" with "'" and I find on google the solutions.

I have this version of python:

user@ubuntu:/media/DATA/prototi/prototypefin4$ python --version
Python 2.7.4

I try for replace and info:

strg = jsondict.replace("u'", "'")
        print "\n\n\n\n\n\n\n\n\n\n\n"
        print strg 
        print "\n\n\n\n\n\n\n"

And with my server in cherrypy I have this error:

    Traceback (most recent call last):
  File "/usr/lib/python2.7/dist-packages/cherrypy/_cprequest.py", line 656, in respond
    response.body = self.handler()
  File "/usr/lib/python2.7/dist-packages/cherrypy/lib/encoding.py", line 188, in __call__
    self.body = self.oldhandler(*args, **kwargs)
  File "/usr/lib/python2.7/dist-packages/cherrypy/_cpdispatch.py", line 34, in __call__
    return self.callable(*self.args, **self.kwargs)
  File "web_editormy.py", line 585, in save_demo
    strg = jsondict.replace("u'", "'")
AttributeError: 'dict' object has no attribute 'replace'

This is the variable jsondict:

{u'demo_title': u'Demo title', u'proc1_script': u'script.sh parameters', u'inputp3_id': u'pepepe', u'outputp2_value': u'boh', u'demo_input_description': u'hola mundo', u'titleimg3': u'Gardens', u'outputp4_visible': u'on'}

And I want to delete this horror of u

Since I print this variable's contents jsondict into a file. Therefore it is more agreeable that there is not this u

Why not function the replace?

Miss the libraries of python?

These are that I loaded

   # -*- coding: utf-8 -*-

import urllib


import hashlib
from datetime import datetime
from random import random

#################################################

import json
from StringIO import StringIO

import re

#################################################

from mako.template import Template
from mako.lookup import TemplateLookup
from mako.exceptions import RichTraceback

#################################################

import os, shutil
from lib import index_dict, http_redirect_303

import zipfile
import sys

######################3

import cherrypy
from cherrypy.lib.static import serve_file

from config import file_dict

Where I wrong?

2 Answers2

1

jsondict is a dict which you store the data ? i search all the attribute of dict, there is not a arrtribute named 'replace'.So, you may need to read the data out from the dict as string , then use the string's method 'replace' to replace the "u'" with "'".

some misunderstood of what you are trying to do.actually , "u'" is not a part of the value of the dict,it means the str is unicode.if you want to delete the "u'" ,may do like this : dict['key'] = dict['key'].encode('utf-8'),you need to ergodic the whole jsondict.

nico zhang
  • 51
  • 6
1

The u'' is just a unicode literal, if you are seeing this is because you are getting the representation of a python value, not the value.

To generate the JSON representation a the python dictionary just do:

json_string = json.dumps(jsondict)
with open('output.json', 'w') as outfile:
    outfile.write(json_string)

or better:

with open('output.json', 'w') as outfile:
     json.dump(jsondict, outfile)
cyraxjoe
  • 5,661
  • 3
  • 28
  • 42