我得到一个包含图像字节的json数组。这个json数组嵌入在json对象中。当检查这个json数组中的元素类型时,它显示了类' str'。
我想将这些字节从json数组保存到不同的文件中。这就是我所做的。
images = []
images = data["Images"]
del data["Images"]
future = db.Document.insert(data)
result = yield future
if result:
index = 1
for image in images:
os.mkdir("/home/sandeep/Projects/PycharmProjects/Fortentia/src/Images/QID"+str(result))
f = open("/home/sandeep/Projects/PycharmProjects/Fortentia/src/Images/QID"+str(result)+"option"+str(index)+".jpg","wb")
f.write(image)
f.close()
index += 1
self.write("successful")
else:
self.write("Unsuccessful")
我正在和Motor一起使用龙卷风。
我得到的错误是:
[E 160209 20:27:17 web:1524] Uncaught exception POST /question (192.168.0.103)
HTTPServerRequest(protocol='http', host='192.168.0.2:8000', method='POST', uri='/question', version='HTTP/1.1', remote_ip='192.168.0.103', headers={'Accept-Encoding': 'gzip', 'Content-Type': 'application/json; charset=utf-8', 'Host': '192.168.0.2:8000', 'Connection': 'Keep-Alive', 'User-Agent': 'Dalvik/2.1.0 (Linux; U; Android 5.1; Google Nexus 4 - 5.1.0 - API 22 - 768x1280 Build/LMY47D)', 'Content-Length': '206'})
Traceback (most recent call last):
File "/usr/local/lib/python3.4/dist-packages/tornado/web.py", line 1445, in _execute
result = yield result
File "/usr/local/lib/python3.4/dist-packages/tornado/gen.py", line 1008, in run
value = future.result()
File "/usr/local/lib/python3.4/dist-packages/tornado/concurrent.py", line 232, in result
raise_exc_info(self._exc_info)
File "<string>", line 3, in raise_exc_info
File "/usr/local/lib/python3.4/dist-packages/tornado/gen.py", line 1017, in run
yielded = self.gen.send(value)
File "/home/sandeep/Projects/PycharmProjects/Fortentia/src/Question.py", line 32, in post
f.write(image)
TypeError: 'str' does not support the buffer interface
提前致谢。