如果之前已经提出过这个问题我会道歉.我还不清楚python3.2中的编码.
我正在阅读一个csv(用UTF-8编码,没有BOM),我在csv中有法语口音.
这是打开和读取csv文件的代码:
csvfile = open(in_file, 'r', encoding='utf-8')
fieldnames = ("id","locale","message")
reader = csv.DictReader(csvfile,fieldnames,escapechar="\\")
for row in reader:
if row['id'] == id and row['locale'] == locale:
out = row['message'];
Run Code Online (Sandbox Code Playgroud)
我正在以Json的身份返回消息(out)
jsonout = json.dumps(out, ensure_ascii=True)
return HttpResponse(jsonout,content_type="application/json; encoding=utf-8")
Run Code Online (Sandbox Code Playgroud)
但是,当我预览结果时,我会将重音e(法语)替换为\ u00e9.
你能告诉我我做错了什么,我应该怎么做,以便json输出显示带有重音的正确e.
谢谢
原始文本文件“chinese.txt”如下
\n{"type":"FeatureCollection","text":"\xe4\xbd\xa0\xe5\xa5\xbd"}
在 Mac 上,在终端中运行命令,如下所示
\n$ cat chinese.txt | python -m json.tool
输出是
\n{\n "text": "\\u4f60\\u597d",\n "type": "FeatureCollection"\n}\nRun Code Online (Sandbox Code Playgroud)\n如何添加参数以避免“\\u4f60\\u597d”并获得“\xe4\xbd\xa0\xe5\xa5\xbd”
\n我喜欢做的是从 shell 中使用python -m json.tool而不修改json.tool. 一个常见的用例是重新格式化 UTF-8 编码的 json 文件,并保留中文字符,而不是像 \\uxxxx。
我有一本包含阿拉伯语单词的字典,例如
data = [{'name': '????'}, {'name': '????'}]
print(json.dumps(data), file=open('data.json', 'a', encoding="utf-8"))
Run Code Online (Sandbox Code Playgroud)
输出:
[{"name": "\u0622\u0632\u064e\u0631"}...]
Run Code Online (Sandbox Code Playgroud)
我不想在创建 data.json 文件时对阿拉伯文本进行编码。如果我不使用 json.dumps 那么它工作正常但是它显示单引号 ' 而不是双引号 "
我试图防止valuePOST 请求中的字符串(在本例中为变量)被转义,因为它存储在 JSON 中。我的代码是
def addProduct(request):
if request.POST:
post = {}
for key in request.POST:
value = request.POST[key].encode('utf-8')
try:
value = json.loads(value).encode('utf-8')
except Exception:
pass
post[key] = value.encode('utf-8')
doc = json.dumps(post)
Run Code Online (Sandbox Code Playgroud)
我可以看到的调试value是 unicode 类型,我相信这就是 Django 处理请求对象的方式。实际的字符串,尽管 unicode 在 之前不会对其特殊字符进行转义post[key] = value。如果我尝试更改此设置post[key] = value.encode('utf-8')以防止它被转义,则会收到错误:'ascii' codec can't decode byte 0xe2 in position 38: ordinal not in range(128)
有任何想法吗?
我有一个 python 3 脚本,应该从 .csv 文件获取一些数据并将其写入 json 文件。\n在我的处理过程中,编码是正确的,因此德语元音 \xc3\xbc、\xc3\xa4 或度数符号\xc2\xb0 就像它们一样(#coding=cp1252 在头部)。
\n但是当我通过 json.dump() 编写字典时,编码消失了......
\n如何使用正确的编码将字典写入 json 文件?
\n\n# -*- coding: cp1252 -*-\nimport json\nfrom pandas import read_csv\n\nx={"\xc3\xa4\xc3\xb6": "\xc3\xbc\xc2\xb0"}\nprint(x, json.dumps(x, indent=4))\n\n>>>> {\'\xc3\xa4\xc3\xb6\': \'\xc3\xbc\xc2\xb0\'} {"\\u00e4\\u00f6": "\\u00fc\\u00b0"}\nRun Code Online (Sandbox Code Playgroud)\n #!/usr/bin/env python\n# -*- coding: utf-8 -*-\nimport json\n\n\nd = {'a':'\xd1\x82\xd0\xb5\xd0\xba\xd1\x81\xd1\x82',\n 'b':{\n 'a':'\xd1\x82\xd0\xb5\xd0\xba\xd1\x81\xd1\x822',\n 'b':'\xd1\x82\xd0\xb5\xd0\xba\xd1\x81\xd1\x823'\n }}\nprint(d)\n\nw = open('log', 'w')\njson.dump(d,w, ensure_ascii=False)\nw.close()\nRun Code Online (Sandbox Code Playgroud)\n\n它给了我:\nUnicodeEncodeError: 'ascii' 编解码器无法对位置 1-5 中的字符进行编码:序数不在范围内(128)
\n我试图使用Python请求模块将一些数据和文件发送到我的django rest应用程序但是得到以下错误.
raise MultiPartParserError('Invalid boundary in multipart: %s' % boundary)
MultiPartParserError: Invalid boundary in multipart: None
Run Code Online (Sandbox Code Playgroud)
码:-
import requests
payload={'admins':[
{'first_name':'john'
,'last_name':'white'
,'job_title':'CEO'
,'email':'test1@gmail.com'
},
{'first_name':'lisa'
,'last_name':'markel'
,'job_title':'CEO'
,'email':'test2@gmail.com'
}
],
'company-detail':{'description':'We are a renowned engineering company'
,'size':'1-10'
,'industry':'Engineering'
,'url':'http://try.com'
,'logo':''
,'addr1':'1280 wick ter'
,'addr2':'1600'
,'city':'rkville'
,'state':'md'
,'zip_cd':'12000'
,'phone_number_1':'408-393-254'
,'phone_number_2':'408-393-221'
,'company_name':'GOOGLE'}
}
files = {'upload_file':open('./test.py','rb')}
import json
headers = {'content-type' : 'application/json'}
headers = {'content-type' : 'multipart/form-data'}
#r = requests.post('http://127.0.0.1:8080/api/create-company-profile/',data=json.dumps(payload),headers=headers,files=files)
r = requests.post('http://127.0.0.1:8080/api/create-company-profile/',data=payload,headers=headers,files=files)
print r.status_code
print r.text
Run Code Online (Sandbox Code Playgroud)
Django代码: -
class …Run Code Online (Sandbox Code Playgroud) 我来自这个旧的讨论,但解决方案没有多大帮助,因为我的原始数据编码方式不同:
我的原始数据已经用unicode编码,我需要输出为UTF-8
data={"content":u"\u4f60\u597d"}
当我尝试转换为utf时:
json.dumps(data, indent=1, ensure_ascii=False).encode("utf8")
我得到的输出是
"content": "ä½ å¥½"和预期的输出应该是
"content": "??"
我尝试了没有ensure_ascii=false,输出变得简单未转义"content": "\u4f60\u597d"
如何将以前\ u转义的json转换为UTF-8?