gpt4 book ai didi

python - 不是 JSON 可序列化的

转载 作者:太空宇宙 更新时间:2023-11-04 10:12:36 25 4
gpt4 key购买 nike

按照文档转换API的例子,尝试使用Flask将msword文档转换为文本,但是没有成功。

这是代码

import os, json, requests

from flask import Flask, jsonify

from watson_developer_cloud import DocumentConversionV1

app = Flask(__name__) #create flask instance

@app.route('/')

def Welcome():

v = json.loads(os.getenv('VCAP_SERVICES'))
svcName = 'document_conversion'
svc = v[svcName][0]['credentials']
url = svc['url']
user = svc['username']
password = svc['password']
document_conversion = DocumentConversionV1(username=user, password=password,version='2015-12-15')
# Example of retrieving html or plain text
with open('./doc.docx', 'rb') as document:
config = {'conversion_target': DocumentConversionV1.NORMALIZED_TEXT}
print(json.dumps(document_conversion.convert_document(document=document, config=config),indent=2))

if __name__ == "__main__":

port = os.getenv('VCAP_APP_PORT', '5000')
app.run(host='0.0.0.0', port=int(port),debug=True)

这是运行时日志

File "/home/vcap/app/.heroku/python/lib/python3.5/site-packages/flask/app.py", line 1836, in __call__

return self.wsgi_app(environ, start_response)

File "/home/vcap/app/.heroku/python/lib/python3.5/site-packages/flask/app.py", line 1820, in wsgi_app

response = self.make_response(self.handle_exception(e))

File "/home/vcap/app/.heroku/python/lib/python3.5/site-packages/flask/app.py", line 1403, in handle_exception

reraise(exc_type, exc_value, tb)

File "/home/vcap/app/.heroku/python/lib/python3.5/site-packages/flask/_compat.py", line 33, in reraise

raise value

File "/home/vcap/app/.heroku/python/lib/python3.5/site-packages/flask/app.py", line 1817, in wsgi_app

response = self.full_dispatch_request()

File "/home/vcap/app/.heroku/python/lib/python3.5/site-packages/flask/app.py", line 1477, in full_dispatch_request

rv = self.handle_user_exception(e)

File "/home/vcap/app/.heroku/python/lib/python3.5/site-packages/flask/app.py", line 1381, in handle_user_exception

reraise(exc_type, exc_value, tb)

File "/home/vcap/app/.heroku/python/lib/python3.5/site-packages/flask/_compat.py", line 33, in reraise

raise value

File "/home/vcap/app/.heroku/python/lib/python3.5/site-packages/flask/app.py", line 1475, in full_dispatch_request

rv = self.dispatch_request()

File "/home/vcap/app/.heroku/python/lib/python3.5/site-packages/flask/app.py", line 1461, in dispatch_request

return self.view_functions[rule.endpoint](**req.view_args)

File "/home/vcap/app/welcome.py", line 39, in Welcome

password = svc['password']

document_conversion = DocumentConversionV1(username=user, password=password,version='2015-12-15')

# Example of retrieving html or plain text

with open('./doc.docx', 'rb') as document:

config = {'conversion_target': DocumentConversionV1.NORMALIZED_TEXT}

print(json.dumps(document_conversion.convert_document(document=document, config=config),indent=2))







if __name__ == "__main__":

port = os.getenv('VCAP_APP_PORT', '5000')

File "/home/vcap/app/.heroku/python/lib/python3.5/json/__init__.py", line 237, in dumps

**kw).encode(obj)

File "/home/vcap/app/.heroku/python/lib/python3.5/json/encoder.py", line 201, in encode

chunks = list(chunks)

File "/home/vcap/app/.heroku/python/lib/python3.5/json/encoder.py", line 436, in _iterencode

o = _default(o)

File "/home/vcap/app/.heroku/python/lib/python3.5/json/encoder.py", line 180, in default

raise TypeError(repr(o) + " is not JSON serializable")

最佳答案

文档似乎已过时。返回的对象是 Response对象,如 Requests 中所示图书馆。根据我的经验,您可以只使用 .text 来获取表示 JSON 的字符串。我应该说,您可能必须使用 .json 表示来获取可以调用 .dumps() 的内容。

关于python - <Response [200]> 不是 JSON 可序列化的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37445886/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com