【发布时间】:2019-10-09 11:59:21
【问题描述】:
我正在使用 Google Docs Python API,特别是官方示例Extract text from a document 一切运行良好,最后我将提取的文本打印到终端,这是最后一段代码 main func 的结果
def main():
"""Uses the Docs API to print out the text of a document."""
credentials = get_credentials()
http = credentials.authorize(Http())
docs_service = discovery.build(
'docs', 'v1', http=http, discoveryServiceUrl=DISCOVERY_DOC)
doc = docs_service.documents().get(documentId=DOCUMENT_ID).execute()
doc_content = doc.get('body').get('content')
print(read_strucutural_elements(doc_content))
问题是我在尝试将该文本保存到 .txt 文件时遇到问题,收到此错误消息
f.write(read_strucutural_elements(doc_content)) UnicodeEncodeError: 'ascii' 编解码器无法在位置 64 对字符 u'\xfa' 进行编码:序数 不在范围内(128)
read_structural_elements 调用的返回类型是
print(type(read_strucutural_elements(doc_content)))
<type 'unicode'>
有什么建议吗?
干杯!
【问题讨论】: