最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

解析从node.js收到的JSON对象时,json.loads()给出了UnicodeEncodeError

IT培训 admin 6浏览 0评论

解析从node.js收到的JSON对象时,json.loads()给出了UnicodeEncodeError

我试图从我的node.js服务器发送一些json对象到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

解析从node.js收到的JSON对象时,json.loads()给出了UnicodeEncodeError

我试图从我的node.js服务器发送一些json对象到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
发布评论

评论列表 (0)

  1. 暂无评论