python - json.loads() 解析从 node.js 接收的 JSON 对象时给出 UnicodeEncodeError

标签 python node.js

我正在尝试将一些 json 对象从我的 node.js 服务器发送到 python 脚本。然而,当尝试使用 json.loads 将 json 对象转换为字典时,许多输入都会出现 UnicodeEncodeErrors。我需要做什么才能正确解码 js 对象。

Error: UnicodeDecodeError: 'charmap' codec can't decode byte 0x9d in position 2062: character maps to <undefined>
    at PythonShell.parseError (D:\Users\Temp\Desktop\empman\node_modules\python-shell\index.js:183:17)
    at terminateIfNeeded (D:\Users\Temp\Desktop\empman\node_modules\python-shell\index.js:98:28)
    at ChildProcess.<anonymous> (D:\Users\Temp\Desktop\empman\node_modules\python-shell\index.js:88:9)
    at emitTwo (events.js:106:13)
    at ChildProcess.emit (events.js:191:7)
    at Process.ChildProcess._handle.onexit (internal/child_process.js:219:12)
    at Process.onexit (D:\Users\Temp\Desktop\empman\node_modules\async-listener\glue.js:188:31)
    ----- Python Traceback -----
    File "word.py", line 38, in <module>
      json_data=open('data.txt').read()
    File "D:\Users\Temp\AppData\Local\Programs\Python\Python36-32\lib\encodings\cp1252.py", line 23, in decode
      return codecs.charmap_decode(input,self.errors,decoding_table)[0]

对应的python代码

from docx import Document
from docx.shared import Inches
import sys
import io
import json
document = Document('template.docx')
# newdocument = Document('resume.docx')
# print(sys.argv)  # Note the first argument is always the script filename.
resumearray = [];
for x in range(0, 21):
    resumearray.append(input())
#json_data=open('data.txt').read()
f = io.open('data','r', encoding='utf-16-le')
# #datastore = json.loads(f.read)
print(f.read())
# text = f.read()
# json_data = text

# document.add_paragraph('_______________________________________________________________________')
#document.add_paragraph(resumearray[1])
k=resumearray[1]
#document.add_paragraph(k)
jsobject = json.loads(k)
document.add_paragraph('_______________________________________________')
#document.add_paragraph(jsobject.values())
for x in range(0, 9):
    if resumearray[x]=='[]':
        document.add_paragraph('nothing was found')
    else:
        document.add_paragraph(resumearray[x])

最佳答案

您正在 Windows 上运行 python,默认编码为 cp1252。 json 编码为 utf-8,因此出现错误。

>>> with open('blob.json', encoding='cp1252') as f:
...     j = json.load(f)
... 
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
  File "/usr/local/lib/python3.6/json/__init__.py", line 296, in load
    return loads(fp.read(),
  File "/usr/local/lib/python3.6/encodings/cp1252.py", line 23, in decode
    return codecs.charmap_decode(input,self.errors,decoding_table)[0]
UnicodeDecodeError: 'charmap' codec can't decode byte 0x9d in position 2795: character maps to <undefined>

使用 utf-8 代替:

>>> with open('blob.json', encoding='utf-8') as f:
...     j = json.load(f)
... 
>>> print(len(j))
29

关于python - json.loads() 解析从 node.js 接收的 JSON 对象时给出 UnicodeEncodeError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47845057/

相关文章:

javascript - TypeError : _this. store.getState 不是从 Redux 使用连接时的函数

node.js - GCF Node10 部署失败 : "Function failed on loading user code. Error message: Provided code is not a loadable module."

python - 谷歌静态 map API : representing a route as an encoded polyline

python - 为什么node.js读取文件比python快?

javascript - 尝试返回数据时 var 返回 undefined 并且控制台日志显示数据

node.js - Keycloak更改注册流程/创建用户API

node.js - puppeteer如何获取元素tagName

python - Pandas :合并重复的字符串

python - QPaint 使用 mouseevent 进行平移缩放

python - 有没有办法加速 tf.keras 中的嵌入层?