说我有一个功能:
def NewFunction():
return '£'
Run Code Online (Sandbox Code Playgroud)
我想在它前面打一些带有井号的东西,当我尝试运行这个程序时它会输出错误,显示以下错误信息:
SyntaxError: Non-ASCII character '\xa3' in file 'blah' but no encoding declared;
see http://www.python.org/peps/pep-0263.html for details
Run Code Online (Sandbox Code Playgroud)
谁能告诉我如何在返回功能中加入英镑符号?我基本上是在课堂上使用它,它'__str__'包含在包含英镑符号的部分内.
我正在使用托管公司,如果文件index.html不存在,它将列出目录中的文件,它使用iso-8859-1作为默认编码.
如果服务器是Apache,有没有办法将UTF-8设置为默认值?
更新:另外我发现它实际上是使用HTML 3.2的DOCTYPE,然后根本就没有字符集...所以它没有设置任何编码.但有没有办法改变它使用UTF-8?
有人知道一个好的Emacs模式来编辑JSON吗?我正在开发的一个应用程序使用基于JSON的通信协议,并且数据很好地缩进和语法突出显示将帮助我解决它的过程.
将包含unicode字符的字符串转储为json会产生奇怪的unicode转义序列:
text = "??????"
print(text) # output: ??????
import json
json_text = json.dumps(text)
print(json_text) # output: "\u2302\u2698\u3044\u306e\u6cd5\u5ac1"
Run Code Online (Sandbox Code Playgroud)
我希望得到这个输出:
"??????"
Run Code Online (Sandbox Code Playgroud)
如何将unicode字符转储为字符而不是转义序列?
我的python列表中有以下字符串(从命令提示符处获取):
>>> o['records'][5790]
(5790, 'Vlv-Gate-Assy-Mdl-\xe1M1-2-\xe19/16-10K-BB Credit Memo ', 60,
True, '40141613')
>>>
Run Code Online (Sandbox Code Playgroud)
我试过这里提到的建议:更改Python的默认编码?
进一步将默认编码更改为utf-16.但仍json.dumps()抛出异常如下:
>>> write(o)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "okapi_create_master.py", line 49, in write
o = json.dumps(output)
File "C:\Python27\lib\json\__init__.py", line 231, in dumps
return _default_encoder.encode(obj)
File "C:\Python27\lib\json\encoder.py", line 201, in encode
chunks = self.iterencode(o, _one_shot=True)
File "C:\Python27\lib\json\encoder.py", line 264, in iterencode
return _iterencode(o, 0)
UnicodeDecodeError: 'utf8' codec can't decode byte 0xe1 in position 25: invalid
continuation byte
Run Code Online (Sandbox Code Playgroud)
无法确定这种字符串需要什么样的转换才能 …
我有一个data我存储的字典:
key - 活动的ID
value- 此事件的名称,其中value是UTF-8字符串
现在,我想将此地图写入json文件.我试过这个:
with open('events_map.json', 'w') as out_file:
json.dump(data, out_file, indent = 4)
Run Code Online (Sandbox Code Playgroud)
但这给了我错误:
UnicodeDecodeError:'utf8'编解码器无法解码位置0的字节0xbf:无效的起始字节
现在,我也尝试过:
with io.open('events_map.json', 'w', encoding='utf-8') as out_file:
out_file.write(unicode(json.dumps(data, encoding="utf-8")))
Run Code Online (Sandbox Code Playgroud)
但这会引发同样的错误:
UnicodeDecodeError:'utf8'编解码器无法解码位置0的字节0xbf:无效的起始字节
我也尝试过:
with io.open('events_map.json', 'w', encoding='utf-8') as out_file:
out_file.write(unicode(json.dumps(data, encoding="utf-8", ensure_ascii=False)))
Run Code Online (Sandbox Code Playgroud)
但这会引发错误:
UnicodeDecodeError:'ascii'编解码器无法解码位置3114中的字节0xbf:序数不在范围内(128)
关于如何解决这个问题的任何建议?
编辑: 我相信这是导致我这个问题的路线:
> data['142']
'\xbf/ANCT25'
Run Code Online (Sandbox Code Playgroud)
编辑2:
该data变量被从文件中读取.所以,从文件中读取后:
data_file_lines = io.open(file_name, 'r', encoding='utf8').readlines()
Run Code Online (Sandbox Code Playgroud)
然后我做:
with io.open('data/events_map.json', 'w', encoding='utf8') as json_file:
json.dump(data, json_file, ensure_ascii=False)
Run Code Online (Sandbox Code Playgroud)
这给了我错误:
TypeError:必须是unicode,而不是str
然后,我尝试使用数据字典执行此操作:
for tuple in sorted_tuples (the …Run Code Online (Sandbox Code Playgroud) 如何打印带有"à"或"ç"等特殊字符的json?
我可以这样打印:
import json
weird_dict ={"person": "ç", "á": 'à', "ç": 'ã'}
print json.dumps(weird_dict, indent=4, sort_keys=True)
output:
{
"person": "\u00e7",
"\u00e1": "\u00e0",
"\u00e7": "\u00e3"
}
Run Code Online (Sandbox Code Playgroud)
如果我使用'ensure_ascii = False'
weird_dict={"person": "ç", "á": 'à', "ç": 'ã'}
print json.dumps(weird_dict, indent=4, sort_keys=True, ensure_ascii=False)
output:
{
"person": "ç",
"á": "à ",
"ç": "ã"
}
Run Code Online (Sandbox Code Playgroud)
如何使用json克服特殊字符问题?我需要渲染当我使用pystache并尝试打印pystache.render('嗨{{person}}!',weird_dict)它发生在我身上:
"'ascii' codec can't decode byte 0xc3 in position 4770: ordinal not in range(128)"
Run Code Online (Sandbox Code Playgroud) 我正在尝试将一些UTF-8字符输出到JSON文件.
当我保存文件时,它们的编写方式如下:
{"some_key":"Enviar invitaci\u00f3n privada"}
以上内容有效且有效.当我加载文件并打印'some_key'时,它会在终端显示"Enviartunitaciónprivada".
反正有没有用"some_key"作为编码版本编写JSON文件,像这样?
{"some_key":"Enviartinvitaciónprivada"}
这是我的一些代码,我正在尝试将字典转换为包含波斯语字符的 json,但我得到的是问号而不是字符。我的字典看起来像这样:
\n\nbycommunity("0": [{"60357": "\xd8\xa7\xdb\x8c\xd9\x86 \xdb\x8c\xda\xa9 \xd9\xbe\xdb\x8c\xd8\xa7\xd9\x85 \xd8\xa7\xd8\xb3\xd8\xaa"}] )\n\nwith open(\'data.json\', \'wb\') as f:\nf.write(json.dumps(bycommunity).encode("utf-8"))\nRun Code Online (Sandbox Code Playgroud)\n\n结果是:
\n\n{"0": [{"60357": "?????? ??? ??? ???? ???????? ??????"}]} \nRun Code Online (Sandbox Code Playgroud)\n 例如:
>>> print json.dumps('r?že')
"r\u016f\u017ee"
Run Code Online (Sandbox Code Playgroud)
(当然,在真正的程序中,它不仅仅是一个单独的字符串,并且在使用时它也在文件中显示如此json.dump())我希望它输出简单的"růže",如何做到这一点?
json ×8
python ×8
unicode ×6
utf-8 ×2
apache ×1
dictionary ×1
emacs ×1
encoding ×1
persian ×1
pystache ×1
python-2.7 ×1
utf8-decode ×1