Python 如何删除u表示字符串是unicode
我想用Python 如何删除u表示字符串是unicode,python,python-2.7,cherrypy,Python,Python 2.7,Cherrypy,我想用“'”替换字符“u'”,我在谷歌上找到了解决方案 我有这个版本的python: user@ubuntu:/media/DATA/prototi/prototypefin4$ python --version Python 2.7.4 我尝试替换并: 我的服务器是cherrypy,我有一个错误: Traceback (most recent call last): File "/usr/lib/python2.7/dist-packages/cherrypy/_cpreques
“'”
替换字符“u'”
,我在谷歌上找到了解决方案
我有这个版本的python:
user@ubuntu:/media/DATA/prototi/prototypefin4$ python --version
Python 2.7.4
我尝试替换并:
我的服务器是cherrypy,我有一个错误:
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'
这是变量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'}
我想删除这个恐怖的u
因为我将这个变量的内容jsondict
打印到一个文件中。
因此,没有这个u
为什么不更换功能
想念python的库吗
这些是我装的
# -*- 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
我哪里错了?jsondict是存储数据的dict?我搜索dict的所有属性,没有名为“replace”的arproperty。因此,您可能需要将dict中的数据作为字符串读取,然后使用字符串的方法“replace”将“u”替换为“” 有些人误解了你要做的事情。实际上,“u'”不是dict值的一部分,它意味着str是unicode。如果你想删除“u'”,可以这样做:dict['key']=dict['key']。encode('utf-8'),你需要遍历整个jsondict。
u'
只是一个unicode文字,如果您看到这是因为您得到的是python值的表示,而不是值
要在python字典中生成JSON表示,只需执行以下操作:
json_string = json.dumps(jsondict)
with open('output.json', 'w') as outfile:
outfile.write(json_string)
或者更好:
with open('output.json', 'w') as outfile:
json.dump(jsondict, outfile)
jsondict
是字典而不是字符串OK。。还有一种可能是替换字符,因为我不喜欢字符u'
,因为u
不是字符串中的字符,u
表示字符串是unicode。你不“喜欢”它是什么意思?@DavidRobinson我想他指的是额外的空格,我不知道这个指示。因为我将这个变量的内容jsondict
打印到一个文件中。因此,更令人满意的是,没有这个u
。UEEE我认为可以禁用u的指示?3.工程量相等。。
with open('output.json', 'w') as outfile:
json.dump(jsondict, outfile)